let x = 10;
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
x *= 2; // x = x * 2 → 24
x /= 4; // x = x / 4 → 6
x %= 4; // x = x % 4 → 2
Comparison Operators
Compare values and return true or false:
let a = 5;
let b = "5";
console.log(a == b); // true - Equal (value only)
console.log(a === b); // false - Strict equal (value and type)
console.log(a != b); // false - Not equal
console.log(a !== b); // true - Strict not equal
console.log(a > 3); // true - Greater than
console.log(a < 3); // false - Less than
console.log(a >= 5); // true - Greater than or equal
console.log(a <= 5); // true - Less than or equal
Operator
Description
Example
==
Equal (loose)
5 == "5" → true
===
Equal (strict)
5 === "5" → false
!=
Not equal
5 != 3 → true
!==
Strictly not equal
5 !== "5" → true
>
Greater than
5 > 3 → true
<
Less than
5 < 3 → false
Logical Operators
Combine boolean values:
AND (&&)
True if both conditions are true:
let age = 25;
let hasLicense = true;
if (age >= 18 && hasLicense) {
console.log("Can drive");
}
OR (||)
True if at least one condition is true:
let isWeekend = false;
let isHoliday = true;
if (isWeekend || isHoliday) {
console.log("No work today");
}
NOT (!)
Reverses the boolean value:
let isRaining = true;
if (!isRaining) {
console.log("Bring umbrella");
} else {
console.log("Don't need umbrella");
}
If / Else Statements
Execute code conditionally:
Simple If
if (age >= 18) {
console.log("You are an adult");
}
If / Else
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
If / Else If / Else
if (grade >= 90) {
console.log("A");
} else if (grade >= 80) {
console.log("B");
} else if (grade >= 70) {
console.log("C");
} else {
console.log("F");
}
Switch Statement
Execute different code for different values:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown";
}
console.log(dayName); // "Wednesday"
Ternary Operator
Shorthand for if/else:
let age = 20;
// Long way
let status;
if (age >= 18) {
status = "Adult";
} else {
status = "Minor";
}
// Short way with ternary
let status = age >= 18 ? "Adult" : "Minor";
Increment and Decrement
let count = 5;
count++; // 6 - Increment by 1
count--; // 5 - Decrement by 1
++count; // 6 - Increment before returning
count++; // 7 - Increment after returning
Loops
For Loop
for (let i = 0; i < 5; i++) {
console.log(i); // Prints 0, 1, 2, 3, 4
}
While Loop
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Do While Loop
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
For Each (Array)
let fruits = ["Apple", "Banana", "Orange"];
fruits.forEach(function(fruit) {
console.log(fruit);
});
Operator Precedence
Operations are evaluated in this order (highest to lowest):
Parentheses: ()
Exponentiation: **
Multiplication/Division/Modulo: *, /, %
Addition/Subtraction: +, -
Comparison: <, >, <=, >=
Equality: ==, ===, !=, !==
Logical AND: &&
Logical OR: ||
Assignment: =, +=, -=, etc.
let result = 2 + 3 * 4; // 14 (not 20) - multiplication first
let result = (2 + 3) * 4; // 20 - parentheses first