| JavaScript Data Types | Strings & Template Literals | |
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
Used to perform basic mathematical operations.
+ Addition- Subtraction* Multiplication/ Division% Modulus (remainder)** Exponentiation (power)++ Increment-- Decrementlet 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
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 assignlet 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);
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 tolet 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
Used to combine or invert Boolean values.
&& AND → true if both are true|| OR → true if at least one is true! NOT → inverts the valuelet isLoggedIn = true; let isAdmin = false; console.log(isLoggedIn && isAdmin); // false console.log(isLoggedIn || isAdmin); // true console.log(!isLoggedIn); // false
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 0let 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
Used to join or append strings.
+ Concatenation+= Append and assignlet first = "Hello"; let second = "World"; console.log(first + " " + second); // "Hello World" let msg = "Hi"; msg += " Shivshanker"; console.log(msg); // "Hi Shivshanker"
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"
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
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.
JavaScript operators can be classified into 9 main types:
| JavaScript Data Types | Strings & Template Literals | |