Types of Operators in Javascript
3 min readJun 14, 2023
1. Arithmetic Operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Increment (++)
- Decrement ( * )
let a = 5 + 3; // Addition: a = 8
let b = 10–4; // Subtraction: b = 6
let c = 4 * 3; // Multiplication: c = 12
let d = 10 / 2; // Division: d = 5
let e = 10 % 3; // Modulus: e = 1
let f = 2; f++; // Increment: f = 3
let g = 5; g — ; // Decrement: g = 4
Note :- Division by zero (e.g., let x = 10 / 0;
) will result in Infinity
or -Infinity
. Modulus operator can give unexpected results with negative numbers.
2. Assignment Operators:
- Assignment (=)
- Addition assignment (+=)
- Subtraction assignment (-=)
- Multiplication assignment (*=)
- Division assignment (/=)
- Modulus assignment (%=)
let a = 5; // Assignment
let b = 10;
b += a; // Addition assignment: b = 15
let c = 7;
c -= 3; // Subtraction assignment: c = 4
let d = 2;
d *= 4; // Multiplication assignment: d = 8
let e = 10;
e /= 5; // Division assignment: e = 2
let f = 7;
f %= 3; // Modulus assignment: f = 1
3. Comparison Operators:
- Equal to (==)
- Not equal to (!=)
- Strict equal to (===)
- Strict not equal to (!==)
- Greater than (>)
- Greater than or equal to (>=)
- Less than (<)
- Less than or equal to (<=)
let a = 5; // Assignment
let b = 10;
b += a; // Addition assignment: b = 15
let c = 7;
c -= 3; // Subtraction assignment: c = 4
let d = 2;
d *= 4; // Multiplication assignment: d = 8
let e = 10;
e /= 5; // Division assignment: e = 2
let f = 7;
f %= 3; // Modulus assignment: f = 1
3. Logical Operators:
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
let a = true;
let b = false;
let logicalAnd = a && b; // Logical AND: logicalAnd = false
let logicalOr = a || b; // Logical OR: logicalOr = true
let logicalNot = !a; // Logical NOT: logicalNot = false
3. Bitwise Operators:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Left shift (<<)
- Right shift (>>)
- Zero-fill right shift (>>>)
let a = 5;
let b = 3;
let bitwiseAnd = a & b; // Bitwise AND: bitwiseAnd = 1
let bitwiseOr = a | b; // Bitwise OR: bitwiseOr = 7
let bitwiseXor = a ^ b; // Bitwise XOR: bitwiseXor = 6
let bitwiseNot = ~a; // Bitwise NOT: bitwiseNot = -6
let leftShift = a << 1; // Left shift: leftShift = 10
let rightShift = a >> 1; // Right shift: rightShift = 2
- Unary Operators:
- Unary plus (+)
- Unary minus (-)
- Logical NOT (!)
- Increment (++)
- Decrement ( — )
- typeof
- delete
let a = 5;
let unaryPlus = +a; // Unary plus: unaryPlus = 5
let unaryMinus = -a; // Unary minus: unaryMinus = -5
let logicalNot = !a; // Logical NOT: logicalNot = false
let preIncrement = ++a; // Pre-increment: preIncrement = 6
let preDecrement = --a; // Pre-decrement: preDecrement = 5
let typeofOperator = typeof a; // typeof: typeofOperator = "number"
- Ternary Operator:
- Conditional operator (a ? b : c)
let a = 5;
let b = 3;
let max = (a > b) ? a : b; // Conditional operator: max = 5
- String Operators:
- Concatenation (+)
- String assignment (+=)
let a = “Hello”;
let b = “ world!”;
let concatenated = a + b; // Concatenation: concatenated = “Hello world!”
let stringAssignment = a += b; // String assignment: stringAssignment = “Hello world!”