← Back to Chapters

JavaScript Operators

? JavaScript Operators

? Quick Overview

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.

  • Arithmetic operators – perform mathematical calculations.
  • Assignment operators – assign or update values.
  • Comparison operators – compare two values and return a boolean.
  • String operators – mainly concatenation using +.
  • Logical operators – combine or invert boolean values.
  • Bitwise operators – operate on the binary representation of numbers.
  • Ternary operator – shorthand for simple if-else.
  • Type operators – check or inspect the data type of a value.

? Key Concepts

  • Operands are the values/variables on which operators work (e.g., in a + b, a and b are operands).
  • Unary operators work with one operand (e.g., ++a, !flag).
  • Binary operators work with two operands (e.g., a + b, a && b).
  • Operator precedence decides which operator runs first when multiple are used in an expression.
  • Associativity decides the direction of evaluation (left-to-right or right-to-left).
  • Short-circuiting with logical operators can skip evaluation of the second operand.

? Syntax & Theory

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.

? Operator Types & Examples

➕ Arithmetic Operators

Used to perform mathematical calculations on numbers.

? View Arithmetic Operators Code
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)

? Assignment Operators

Used to assign and update values stored in variables.

? View Assignment Operators Code
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

⚖️ Comparison Operators

Used to compare two values and return a boolean (true or false).

? View Comparison Operators Code
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

? String Operators

The + operator can concatenate (join) strings. += can append to an existing string.

? View String Operators Code
let first = "Hello";
let second = "World";
console.log(first + " " + second); // "Hello World"

⚙️ Logical Operators

Used mainly with boolean values to combine or invert conditions.

? View Logical Operators Code
let a = true, b = false;
console.log(a && b); // false (AND)
console.log(a || b); // true  (OR)
console.log(!a);     // false (NOT)

? Bitwise Operators

Operate on 32-bit binary representations of numbers (useful for low-level tasks, flags, and masks).

? View Bitwise Operators Code
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)

❔ Ternary Operator

A compact way to write simple if-else logic in a single expression.

? View Ternary Operator Code
let age = 18;
let type = (age >= 18) ? "Adult" : "Minor";
console.log(type); // "Adult"

? Type Operators

Used to identify or inspect the type of a value.

? View Type Operators Code
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)

? Live Output & Explanation

If you run the above code snippets in the browser console or a JS environment:

  • Arithmetic examples will print numeric results like 13, 7, 30, etc.
  • Comparison operators will print true or false based on the comparison.
  • Logical operators will show how conditions combine (e.g., true && false becomes false).
  • The ternary example will print "Adult" because age is 18.
  • The 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.

? Tips & Best Practices

  • Prefer === and !== over == and != to avoid unexpected type coercion.
  • Use the ternary operator for simple conditions, but avoid nesting it too deeply (that hurts readability).
  • Remember that bitwise operators work on 32-bit integers and can give surprising results for large numbers.
  • Be aware that typeof null returns "object" for historical reasons.
  • Logical operators && and || return one of the operands, not always a boolean (useful for default values).

? Try It Yourself

  • Open the browser console and test different arithmetic operations (addition, subtraction, modulus, exponentiation).
  • Use == and === with combinations like 0, "0", false, and compare the results.
  • Write a condition using the ternary operator to check if a number is even or odd.
  • Use typeof on values like arrays, objects, null, and undefined and note the differences.
  • Create a small expression using logical operators to check multiple conditions (e.g., age and country).