| JavaScript Data Types :👈 | 👉:Strings & Template Literals |
JavaScript Operators |
📘 JavaScript Operators
In JavaScript, operators are special symbols used to perform operations on values (operands). For example:
let x = 10 + 5; // here + is an operator, operands are 10 and 5
🔢 Types of Operators
1. Arithmetic Operators
Used to perform basic mathematical operations.
+Addition-Subtraction*Multiplication/Division%Modulus (remainder)**Exponentiation (power)++Increment--Decrement
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
console.log(++a); // 11
console.log(--b); // 2
2. Assignment Operators
Used to assign values to variables, often combined with arithmetic.
=Assign+=Add and assign-=Subtract and assign*=Multiply and assign/=Divide and assign%=Modulus and assign**=Exponentiation and assign
let z = 10;
z += 5; // z = z + 5 → 15
z -= 3; // z = z - 3 → 12
z *= 2; // z = z * 2 → 24
z /= 4; // z = z / 4 → 6
z %= 5; // z = z % 5 → 1
z **= 3; // z = z ** 3 → 1
console.log(z);
3. Comparison Operators
Used to compare two values and return a Boolean (true or false).
==Equal to (loose, allows type conversion)===Strict equal to (no type conversion)!=Not equal!==Strict not equal>Greater than<Less than>=Greater than or equal to<=Less than or equal to
let x = 5, y = "5";
console.log(x == y); // true (type coercion)
console.log(x === y); // false (strict check)
console.log(x != 8); // true
console.log(x !== "5"); // true
console.log(x > 3); // true
console.log(x <= 5); // true
4. Logical Operators
Used to combine or invert Boolean values.
&&AND → true if both are true||OR → true if at least one is true!NOT → inverts the value
let isLoggedIn = true;
let isAdmin = false;
console.log(isLoggedIn && isAdmin); // false
console.log(isLoggedIn || isAdmin); // true
console.log(!isLoggedIn); // false
5. Bitwise Operators
Bitwise operators work on numbers at the binary (bit) level.
&AND → 1 if both bits are 1|OR → 1 if at least one bit is 1^XOR → 1 if bits are different~NOT → inverts all bits<<Left shift → shifts bits left, adds 0s on right>>Right shift → shifts bits right, keeps sign>>>Unsigned right shift → shifts bits right, fills with 0
let a = 5; // 0101
let b = 1; // 0001
console.log(a & b); // 1
console.log(a | b); // 5
console.log(a ^ b); // 4
console.log(~a); // -6
console.log(a << 1); // 10
console.log(a >> 1); // 2
console.log(a >>> 1);// 2
6. String Operators
Used to join or append strings.
+Concatenation+=Append and assign
let first = "Hello";
let second = "World";
console.log(first + " " + second); // "Hello World"
let msg = "Hi";
msg += " Shivshanker";
console.log(msg); // "Hi Shivshanker"
7. Ternary / Conditional Operator
A shorthand for if...else. Syntax: condition ? exprIfTrue : exprIfFalse
let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // "Adult"
// Nested ternary
let marks = 75;
let grade = (marks >= 90) ? "A" : (marks >= 60) ? "B" : "C";
console.log(grade); // "B"
8. Type Operators
Used to check types of values.
typeof→ returns type as a stringinstanceof→ checks if object is instance of a class
console.log(typeof "Hello"); // "string"
console.log(typeof 123); // "number"
console.log(typeof true); // "boolean"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
let arr = [1,2,3];
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true
9. ES2020 Operators: Nullish Coalescing Operator (??)
- ?? → nullish coalescing (returns right-hand side if left-hand side is null/undefined)
- ?. → optional chaining (safely access nested properties)
Returns the right-hand value only if the left-hand value is null or undefined.
Unlike ||, it does not treat 0 or "" as falsy.
let user = null;
console.log(user ?? "Guest"); // "Guest"
let count = 0;
console.log(count ?? 10); // 0 (keeps valid 0)
let text = "";
console.log(text ?? "Default"); // "" (keeps empty string)
Allows safe access to nested object properties, returning undefined if any part is null or undefined.
✅ Summary
JavaScript operators can be classified into 9 main types:
- Arithmetic → Math operations
- Assignment → Assign/update values
- Comparison → Compare values (true/false)
- Logical → Combine conditions
- Bitwise → Work at binary level (&, |, ^, ~, <<,>>, >>>)
- String → Concatenate or append (+, +=)
- Ternary → Shorthand conditional (condition ? true : false)
- Type → Check type (typeof, instanceof)
- ES2020 Coalescing (??) → Default only for null/undefined
| JavaScript Data Types :👈 | 👉:Strings & Template Literals |