JavaScript
To print we use console.log(“Text”);
While JavaScript interpreters can often infer where a statement ends even without a
semicolon, known as Automatic Semicolon Insertion (ASI), it's considered good
practice to include them for clarity and to prevent any unforeseen issues, especially in
complex scenarios.
Comments: // single line) OR /* */ (multiple line)
Scope Hoisting Redeclaration Use
Let Block- Hoisted but Not allowed Preferred for
scoped {}. not in the same variables that
initialized scope. will change.
(temporal
dead zone).
Var Function- Hoisted to Allowed Older way;
scoped. the top of its within the mostly
function same scope. avoided now.
(initialized
as
undefined)
Const Block- Same as let Redeclaration For
scoped {}. (temporal is not allowed. constants.
dead zone). Reassignment But for
is not allowed. objects and
Value must be arrays, const
assigned once prevents
and can not be reassignment
changed. , not
modification.
Start variable names with letters, underscores (_), or dollar signs ($), and avoid reserved
words like const, let, and similar.
Basic Data types: Number, String, Boolean, null, and undefined.
Number 10, 3.5, -2.7
String “My name”
Boolean True:
True
Non-zero number: 1, -2, 3.6
Non-empty string: “hello”, “0”, “false”
Objects: {}, []
new Date()
Falsey Values:
false
0
-0
0n (BigInt zero)
“”
Null
Undefined
NaN
Null null in JavaScript represents a lack of value for a particular
variable.
undefined undefined, a designation for a variable that is declared but not
assigned a value.
Difference between null and undefined:
Feature undefined null
Set by JavaScript developer
Meaning Declared but not assigned Intentionally no value
Type undefined Object (historical bug)
Used when Missing values, uninitialized vars Clearing value, placeholder
for object
Equality (==) true True
Strict equality false false
(===)
Use case Detecting missing function arguments. Resetting a user’s session or
data.
Comparison Operators: ==, !=, ===, !==, >, <, >=, <=
Difference between == and ===
==: it only checks for the value to be equal, not the data type
===: It checks for the data type, known as strict equality.
Different comparison operators can be combined using AND(&&), OR(||)
Arithmetic Operators: +, -, *, /, **
In division to get only the decimal part (3 of 3.9787) we use Math.floor(2/5)
If star is a string ‘5’, to convert it into a number, we use Number(star)
To convert a number variable into a string type, we use String(name)
To check the data type of a variable we use console.log(typeof variable)
Since alphabetical strings can not be converted into a string, we get Nan
How are these other data types converted into Boolean? we use Boolean(variable)
For conditional rendering, we use either if, else if, else or ternary operators (condition?
Option if true: Option if false).
To join strings, we use + and contact
LISTS AND ARRAYS
There are two ways to define an array, using new Array(), or [] brackets.
We can navigate through different elements using index numbers, starting from 0. Also,
we can find the index number of n elements using indexOf(“element”)
To get the length of the array, we use array_name.length
To replace a value, we write the name of the array with the index number in square
brackets, then add an equals, and on the other side, the new value.
Now, if I want to update (add something new to the previous value), without changing it
completely.
To add a new value at the end of the list, we use push, and to remove the value we sue
pop.