









JavaScript Operators
Arithmetic, Comparison, Logical, and More
Introduction to JavaScript Operators
JavaScript operators allow you to perform operations on variables and values. From basic math to complex logic evaluation, operators play a central role in making decisions, transforming data, and controlling program flow.
1. Arithmetic Operators
Used to perform basic math operations.
let a = 10;
let b = 3;
console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Modulus:", a % b);
console.log("Exponentiation:", a ** b);
console.log("Increment a:", ++a);
console.log("Decrement b:", --b);
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Exponentiation: 1000
Increment a: 11
Decrement b: 2
2. Assignment Operators
Used to assign values to variables. Some also perform operations.
let x = 5;
x += 3; // Same as x = x + 3
x -= 2;
x *= 2;
x /= 3;
x %= 2;
Final value of x: 2
3. Comparison Operators
Compare two values and return a boolean (true/false).
console.log(10 == '10'); // Loose equality (true)
console.log(10 === '10'); // Strict equality (false)
console.log(5 != 7); // true
console.log(5 !== '5'); // true
console.log(5 > 3); // true
console.log(5 >= 5); // true
console.log(2 < 4); // true
console.log(2 <= 1); // false
true
false
true
true
true
true
true
false
4. Logical Operators
Used for combining multiple conditions or expressions.
let a = true;
let b = false;
console.log(a && b); // AND
console.log(a || b); // OR
console.log(!a); // NOT
false
true
false
5. Bitwise Operators
Operate on binary representations of numbers.
let x = 5; // 0101
let y = 3; // 0011
console.log(x & y); // AND -> 0001 => 1
console.log(x | y); // OR -> 0111 => 7
console.log(x ^ y); // XOR -> 0110 => 6
console.log(~x); // NOT -> 1010 => -6 (2's complement)
console.log(x << 1); // Left shift => 1010 => 10
console.log(x >> 1); // Right shift => 0010 => 2
1
7
6
-6
10
2
6. Type Operators
Used to check the type of a variable or to determine if an object is an instance of a class.
let name = "Alice";
let age = 30;
console.log(typeof name); // string
console.log(typeof age); // number
class Person {}
let p = new Person();
console.log(p instanceof Person); // true
string
number
true
7. Ternary Operator (?:)
A shorthand for if...else
conditions.
let score = 85;
let result = (score >= 60) ? "Pass" : "Fail";
console.log(result);
Pass
8. Nullish Coalescing Operator (??)
Returns the right-hand operand when the left is null
or undefined
.
let userName = null;
let displayName = userName ?? "Guest";
console.log(displayName);
Guest
9. Optional Chaining Operator (?.)
Safely access deeply nested properties without errors if intermediate is null or undefined.
let user = {
profile: {
name: "John"
}
};
console.log(user.profile?.name); // John
console.log(user.settings?.theme); // undefined (no error)
John
undefined
Conclusion
Understanding JavaScript operators helps you control your code with precision and logic. Whether you're evaluating expressions, comparing values, or chaining safe access, these operators empower you to write efficient and readable code. Practice is key — try modifying the examples and observe how changes affect the output.