JavaScript Operators
JavaScript Operators
An operators are used to perform a mathematical operation on single or multiple operands. There are various operators available in JavaScript, which are categorized into 5 parts-
Arithmetic Operators:-
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder of a division) |
| ++ | Increment |
| __ | Decrement |
Assignment Operators:-
| Operator | Description |
|---|---|
| = | Assign |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
| %= | Modulus and assign |
Comparison Operators:-
| Operator | Description |
|---|---|
| == | Equal to |
| === | Identical (Equal value and equal type) |
| != | Not equal to |
| !== | Not identical (Not equal value or not equal type) |
| > | Greater than |
| >= | Greater than or equal to |
| < | Less than |
| <= | Less than or equal to |
Boolean or Logical Operators:-
| Operator | Description |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
String Operators:-
| Operator | Description |
|---|---|
| = | Assignment |
| + | Concatenate (Join two strings together) |
| += | Concatenate and assign |
Operators in Action - Code Examples
let a = 10, b = 3;
// Arithmetic
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1 (remainder)
console.log(a ** b); // 1000 (exponentiation ES7)
console.log(++a); // 11 (pre-increment)
console.log(b--); // 3 (post-decrement, b becomes 2)
// Assignment
let x = 5;
x += 3; console.log(x); // 8
x -= 2; console.log(x); // 6
x *= 4; console.log(x); // 24
x /= 6; console.log(x); // 4
x **= 2; console.log(x); // 16
Comparison and Equality Operators
Always prefer === (strict equality) over == (loose equality) to avoid unexpected type coercion bugs.
console.log(5 == '5'); // true (loose - type coercion)
console.log(5 === '5'); // false (strict - different types)
console.log(5 != '5'); // false
console.log(5 !== '5'); // true
console.log(10 > 5); // true
console.log(10 >= 10); // true
console.log(3 < 5); // true
console.log(3 <= 2); // false
// Nullish coalescing (ES2020)
const name = null ?? 'Guest';
console.log(name); // Guest
// Optional chaining (ES2020)
const user = { profile: { age: 25 } };
console.log(user?.profile?.age); // 25
console.log(user?.address?.city); // undefined (no error)
Logical and Bitwise Operators
// Logical AND, OR, NOT
console.log(true && false); // false
console.log(true || false); // true
console.log(!true); // false
// Short-circuit evaluation
const isLoggedIn = true;
isLoggedIn && console.log('Welcome back!'); // Welcome back!
const config = null;
const port = config || 3000;
console.log(port); // 3000
// Logical assignment (ES2021)
let a = null;
a ??= 'default'; // assign only if null/undefined
console.log(a); // default
let b = 0;
b ||= 42; // assign if falsy
console.log(b); // 42
// Ternary operator
const age = 20;
const status = age >= 18 ? 'Adult' : 'Minor';
console.log(status); // Adult
Key Takeaways
- Always use === (strict equality) instead of == to avoid type coercion surprises.
- The % modulus operator returns the remainder of division - useful for even/odd checks.
- The ** exponentiation operator (ES7) replaces Math.pow(a, b).
- Short-circuit evaluation: && stops at first falsy, || stops at first truthy.
- The nullish coalescing operator ?? only falls back when the value is null or undefined (not 0 or empty string).
- Optional chaining ?. safely accesses nested properties without throwing if a parent is null/undefined.
Related JavaScript Topics