JavaScript Basics
1. Data Types
JavaScript has two categories of data types:
Primitive Types
String → "Hello"
Number → 42
Boolean → true or false
Null → null
Undefined → undefined
Symbol → Symbol("id")
BigInt → 12345678901234567890n
Non-Primitive (Reference) Types
Object → { key: "value" }
Array → [1, 2, 3]
Function → function() {}
2. let, const, var
var
Function-scoped
Allows redeclaration
Hoisted (initialized as undefined)
let
Block-scoped
Cannot be redeclared in the same scope
Hoisted but in temporal dead zone
const
Block-scoped
Cannot be reassigned
Must be initialized at declaration
Example:
var x = 10;
let y = 20;
const z = 30;
3. Operators
Arithmetic → + - * / % ++ --
Assignment → = += -= *= /=
Comparison → == != === !== > < >= <=
Logical → && || !
Ternary → condition ? value1 : value2
Example:
let result = (a > b) ? "A is greater" : "B is greater";
4. Conditional Statements
if statement
if (x > 10) {
[Link]("Greater than 10");
}
if-else
if (x > 10) {
[Link]("Greater");
} else {
[Link]("Smaller or equal");
}
if-else if ladder
if (x > 20) {
[Link]("Big");
} else if (x > 10) {
[Link]("Medium");
} else {
[Link]("Small");
}
switch statement
switch(day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
default: [Link]("Invalid");
}
5. Loops
while loop
let i = 0;
while (i < 5) {
[Link](i);
i++;
}
do-while loop
let i = 0;
do {
[Link](i);
i++;
} while (i < 5);
for loop
for (let i = 0; i < 5; i++) {
[Link](i);
}
for...in (iterate over object keys)
for (let key in obj) {
[Link](key, obj[key]);
}
for...of (iterate over iterable values like arrays)
for (let value of arr) {
[Link](value);
}