JavaScript Operators
1) Arithmetic Operators
Perform math on numbers. Common operators are: + add, - subtract,
* multiply, / divide, % remainder (modulus),
** exponent (power). ++ and -- increase/decrease by 1.
// + - * / % ** ++ --
let a = 10, b = 3;
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
console.log(a ** b); // 1000 (10³)
2) Assignment Operators
Store values in variables. Combined assignments update and assign in one step:
+=, -=, *=, /=, %=, **=.
let x = 5;
x += 3; // 8
x **= 2; // 64
3) Comparison Operators
Compare two values and return true or false.
Prefer strict comparisons (===, !==) to avoid auto type conversion.
// == vs ===, != vs !==, <, >, <=, >=
console.log('5' == 5); // true (coercion)
console.log('5' === 5); // false (strict)
4) Logical Operators
Combine boolean results. && (AND) returns the first falsy or last value.
|| (OR) returns the first truthy value. ! flips a boolean.
These operators are short-circuiting.
const isLoggedIn = true;
const isAdmin = false;
console.log(isLoggedIn && isAdmin); // false
console.log(isLoggedIn || isAdmin); // true
5) Nullish Coalescing (??) & Optional Chaining (?.)
Use ?. to safely access deep properties (it returns undefined instead of throwing).
Use ?? to provide a default only when the left side is null or undefined
(it does not replace valid values like 0 or '').
const user = { profile: { name: 'Sonu' } };
const nick = user.profile?.nickname ?? 'Guest';
// nick = "Guest"
6) Ternary Operator
Shorthand if/else that returns one of two values based on a condition:
condition ? valueIfTrue : valueIfFalse.
const age = 18;
const canVote = age >= 18 ? 'Yes' : 'No';
7) typeof & instanceof
typeof tells the general type of a value (with a few quirks like
typeof null === 'object'). instanceof checks if an object
comes from a specific constructor (useful for arrays, dates, custom classes).
typeof 'hi'; // "string"
[] instanceof Array; // true
new Date() instanceof Date; // true
8) Unary + / - and ++ / --
Unary + converts to number; unary - negates.
i++ (post-increment) returns the old value, then increments.
++i (pre-increment) increments first, then returns the new value.
const n = '42';
const num = +n; // 42 (string -> number)
let i = 0;
console.log(i++); // 0 (then i becomes 1)
console.log(++i); // 2
9) Bitwise Operators
Work on the binary representation of 32-bit integers:
& AND, | OR, ^ XOR, ~ NOT,
shifts ««, »», »»».
Useful for low-level flags; not common in everyday JS.
// & | ^ ~ << >> >>>
console.log(5 & 3); // 1
console.log(5 | 3); // 7
10) Operator Precedence
Some operators run before others. For math: ** first, then * / %,
then + -. Use parentheses to control order and improve readability.
// ** > *,/,% > +,-
const v1 = 2 + 3 * 4; // 14
const v2 = (2 + 3) * 4; // 20
=== and !==.
When providing defaults, prefer ?? over || (so that valid values like 0 and '' are not replaced).