1.
Primitives and Objects
Primitive Types: The simplest data types in JavaScript, stored directly in memory. They are
immutable.
Examples: string, number, boolean, null, undefined, symbol, bigint
Objects: Complex data structures that store collections of key-value pairs.
let name = "Ali"; // string
let age = 12; // number
let isStudent = true; // boolean
let person = { name: "Ali", age: 12 }; // object
let numbers = [1, 2, 3]; // array
2. Operators and Expressions
Operators are symbols used to perform operations on values. Here’s a table of some key
operators and their functions:
Operator Function Example
+ Addition 5 + 3 // 8
- Subtraction 5 - 3 // 2
* Multiplication 5 * 3 // 15
/ Division 6 / 3 // 2
% Modulus (Remainder) 5 % 2 // 1
** Exponentiation 2 ** 3 // 8
= Assignment x=5
+= Add & Assign x += 3 // x = x + 3
-= Subtract & Assign x -= 3
== Equal (loose) 5 == '5' // true
=== Equal (strict) 5 === '5' // false
!= Not Equal (loose) 5 != '5' // false
!== Not Equal (strict) 5 !== '5' // true
> Greater Than 5 > 3 // true
< Less Than 5 < 3 // false
>= Greater or Equal 5 >= 5 // true
<= Less or Equal 3 <= 5 // true
&& Logical AND true && false // false
|| Logical OR true || false // true
! Logical NOT !true // false
Expressions are any valid combination of values and operators that produce a value.
let sum = 5 + 3; // 8
let greeting = "Hello" + " World"; // "Hello World"
let check = x > 10; // true or false
3. Conditional Expressions
if (x > 10) {
[Link]("Greater than 10");
} else if (x === 10) {
[Link]("Exactly 10");
} else {
[Link]("Less than 10");
}
Ternary Operator:
let result = (x > 10) ? "Greater" : "Not greater";
4. For Loops
Standard for loop: Repeats a block of code until a condition becomes false.
for (let i = 0; i < 5; i++) {
[Link](i); // 0 to 4
}
for...of loop: Loops over values of an iterable (arrays, strings, etc).
let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
[Link](fruit);
}
for...in loop: Loops over keys (property names) of an object.
let person = { name: "Ali", age: 12 };
for (let key in person) {
[Link](key + ": " + person[key]);
}
5. While Loops
let i = 0;
while (i < 5) {
[Link](i);
i++;
}
do...while loop: Runs at least once, then repeats while condition is true.
let j = 0;
do {
[Link](j);
j++;
} while (j < 5);
6. Functions
Function Declaration:
function greet(name) {
[Link]("Hello " + name);
}
greet("Ali");
Function Expression:
const greet = function(name) {
[Link]("Hello " + name);
};
Arrow Function:
const greet = (name) => {
[Link]("Hello " + name);
};
Return values:
function add(a, b) {
return a + b;
}
let sum = add(2, 3); // sum = 5