Arithmetic Operator:
-
Used for basic math operations.
- Addition +
- Subtraction -
- Multiplication *
- Division /
- Modulus (remainder) %
- Power **
let a = 20;
let b = 4;
// Addition (+)
let addition = a + b;
console.log("Addition (20 + 4):", addition); // 24
// Subtraction (-)
let subtraction = a - b;
console.log("Subtraction (20 - 4):", subtraction); // 16
// Multiplication (*)
let multiplication = a * b;
console.log("Multiplication (20 * 4):", multiplication); // 80
// Division (/)
let division = a / b;
console.log("Division (20 / 4):", division); // 5
// Modulus / Remainder (%)
let modulus = a % b;
console.log("Modulus (20 % 4):", modulus); // 0
// Exponentiation (**)
let exponentiation = a ** b;
console.log("Exponentiation (20 ** 4):", exponentiation); // 160000
Comparison Operators:
- Used to compare value. Returns true or false.
let a = 10;
let b = 20;
let c = "10";
// Equal to (==) - compares value only (type conversion allowed)
console.log("Equal to (10 == 20):", a == b); // false
console.log("Equal to (10 == '10'):", a == c); // true (type coercion)
// Strict Equal to (===) - compares value and type
console.log("Strict Equal (10 === 10):", a === 10); // true
console.log("Strict Equal (10 === '10'):", a === c); // false (different types)
// Not Equal to (!=) - compares value only
console.log("Not Equal (10 != 20):", a != b); // true
console.log("Not Equal (10 != '10'):", a != c); // false (type coercion)
// Strict Not Equal to (!==) - compares value and type
console.log("Strict Not Equal (10 !== 20):", a !== b); // true
console.log("Strict Not Equal (10 !== '10'):", a !== c); // true (different types)
// Greater than (>)
console.log("Greater Than (10 > 20):", a > b); // false
console.log("Greater Than (20 > 10):", b > a); // true
// Less than (<)
console.log("Less Than (10 < 20):", a < b); // true
console.log("Less Than (20 < 10):", b < a); // false
// Greater than or Equal to (>=)
console.log("Greater or Equal (10 >= 10):", a >= 10); // true
console.log("Greater or Equal (10 >= 20):", a >= b); // false
// Less than or Equal to (<=)
console.log("Less or Equal (10 <= 20):", a <= b); // true
console.log("Less or Equal (20 <= 10):", b <= a); // false
Assignment Operators :
let a = 50;
console.log("Assignment (a = 50):", a); // 50
// Addition Assignment (+=) a = a + 10
a += 10;
console.log("Addition Assignment (a += 10):", a); // 60
// Subtraction Assignment (-=) a = a - 5
a -= 5;
console.log("Subtraction Assignment (a -= 5):", a); // 55
// Multiplication Assignment (*=) a = a * 2
a *= 2;
console.log("Multiplication Assignment (a *= 2):", a); // 110
// Division Assignment (/=) a = a / 5
a /= 5;
console.log("Division Assignment (a /= 5):", a); // 22
// Modulus Assignment (%=) a = a % 8
a %= 8;
console.log("Modulus Assignment (a %= 8):", a); // 6
// Exponentiation Assignment (**=) a = a ** 3
a **= 3;
console.log("Exponentiation Assignment (a **= 3):", a); // 216
Logical Operator:
// Logical AND (&&) - returns true only if both operands are true
console.log("AND (true && true):", a && a); // true
console.log("AND (true && false):", a && b); // false
console.log("AND (false && false):", b && b); // false
// Logical OR (||) - returns true if at least one operand is true
console.log("OR (true || false):", a || b); // true
console.log("OR (false || false):", b || b); // false
console.log("OR (true || true):", a || a); // true
// Logical NOT (!) - reverses the boolean value
console.log("NOT (!true):", !a); // false
console.log("NOT (!false):", !b); // true
Type of Operator :
- Returns the data type of a value as a string.
console.log("typeof 42:", typeof 42); // number
console.log("typeof 3.14:", typeof 3.14); // number
console.log("typeof 'hello':", typeof "hello"); // string
console.log("typeof true:", typeof true); // boolean
console.log("typeof undefined:", typeof undefined); // undefined
console.log("typeof 9007199254740991n:", typeof 9007199254740991n); // bigint
Increment & Decrement :
// Post-Increment (a++)
// Uses the current value first, then increments by 1
let a = 5;
console.log("Initial a:", a); // 5
let postInc = a++;
console.log("postInc = a++ (value used):", postInc); // 5 (old value returned)
console.log("a after a++:", a); // 6 (incremented after)
// Pre-Increment (++a)
// Increments first, then uses the new value
let b = 5;
console.log("Initial b:", b); // 5
let preInc = ++b;
console.log("preInc = ++b (value used):", preInc); // 6 (incremented before returning)
console.log("b after ++b:", b); // 6
// Post-Decrement (a--)
// Uses the current value first, then decrements by 1
let c = 10;
console.log("Initial c:", c); // 10
let postDec = c--;
console.log("postDec = c-- (value used):", postDec); // 10 (old value returned)
console.log("c after c--:", c); // 9 (decremented after)
// Pre-Decrement (--a)
// Decrements first, then uses the new value
let d = 10;
console.log("Initial d:", d); // 10
let preDec = --d;
console.log("preDec = --d (value used):", preDec); // 9 (decremented before returning)
console.log("d after --d:", d); //9
Top comments (0)