Operators in JavaScript are special symbols used to perform operations on values and variables. They are the building blocks for calculations, decisions, comparisons, and more.
+.if-else.a + b, a and b are operands).++a, !flag).a + b, a && b).In general, JavaScript expressions follow this pattern:
operand1 operator operand2
Examples:
total = price + tax; → uses assignment and arithmetic operators.isAdult = age >= 18; → uses comparison and assignment.result = condition ? "Yes" : "No"; → uses the ternary operator.Used to perform mathematical calculations on numbers.
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 (exponentiation)
console.log(++a); // 11 (increment)
console.log(--b); // 2 (decrement)
Used to assign and update values stored in variables.
let x = 5;
x += 2; // x = x + 2
x -= 1; // x = x - 1
x *= 3; // x = x * 3
x /= 2; // x = x / 2
x %= 2; // x = x % 2
x **= 3; // x = x ** 3
Used to compare two values and return a boolean (true or false).
console.log(5 == '5'); // true (loose equality)
console.log(5 === '5'); // false (strict equality)
console.log(5 != '5'); // false
console.log(5 !== '5'); // true
console.log(5 > 3); // true
console.log(5 < 3); // false
console.log(5 >= 5); // true
console.log(5 <= 4); // false
The + operator can concatenate (join) strings. += can append to an existing string.
let first = "Hello";
let second = "World";
console.log(first + " " + second); // "Hello World"
Used mainly with boolean values to combine or invert conditions.
let a = true, b = false;
console.log(a && b); // false (AND)
console.log(a || b); // true (OR)
console.log(!a); // false (NOT)
Operate on 32-bit binary representations of numbers (useful for low-level tasks, flags, and masks).
console.log(5 & 1); // 1 (AND)
console.log(5 | 1); // 5 (OR)
console.log(5 ^ 1); // 4 (XOR)
console.log(~5); // -6 (NOT)
console.log(5 << 1); // 10 (left shift)
console.log(5 >> 1); // 2 (right shift)
A compact way to write simple if-else logic in a single expression.
let age = 18;
let type = (age >= 18) ? "Adult" : "Minor";
console.log(type); // "Adult"
Used to identify or inspect the type of a value.
console.log(typeof "Hello"); // "string"
console.log(typeof 123); // "number"
console.log(typeof true); // "boolean"
console.log(typeof {}); // "object"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (special case)
If you run the above code snippets in the browser console or a JS environment:
13, 7, 30, etc.true or false based on the comparison.true && false becomes false)."Adult" because age is 18.typeof examples reveal how JavaScript classifies different values.Understanding the outputs helps you debug and reason about your code more confidently.
Result: Click the button to evaluate 4 + 6 * 2.
=== and !== over == and != to avoid unexpected type coercion.typeof null returns "object" for historical reasons.&& and || return one of the operands, not always a boolean (useful for default values).== and === with combinations like 0, "0", false, and compare the results.typeof on values like arrays, objects, null, and undefined and note the differences.