Skip to content

JavaScript Operators

JavaScript operators are the foundation of performing operations and comparisons in the language. They let you manipulate data, evaluate conditions, and build complex logic. There are four primary types of operators

  1. arithmetic
  2. comparison
  3. logical
  4. assignment.

Arithmetic operators are used to perform mathematical calculations.

let a = 20;
let b = 4;
console.log(a + b); // Addition -> 24
console.log(a - b); // Subtraction -> 16
console.log(a * b); // Multiplication -> 80
console.log(a / b); // Division -> 5
console.log(a % b); // Modulus (remainder) -> 0

Comparison operators are used to compare two values and return a boolean (true or false) result.

let x = 5;
let y = "5";
console.log(x == y); // Equal to (value only) -> true (loose equality, checks only value)
console.log(x === y); // Strict equal to (value + type) -> false (strict equality, checks value and type)
console.log(x != y); // Not equal to (value only) -> false (values are equal)
console.log(x < 10); // Less than -> true
console.log(x > 1); // Greater than -> true

Always prefer using === and !== over == and != to avoid unexpected type coercion issues.

coercion is the process of converting a value from one data type to another, such as converting a string to a number or a boolean to a string. Coercion can happen implicitly (automatically by JavaScript) or explicitly (manually by the developer).

Occurs when JavaScript automatically converts one data type to another to execute an operation.

// String to Number
console.log("5" * 2); // 10 (string "5" is coerced to number 5)
// Number to String
console.log("Age: " + 30); // "Age: 30" (number 30 is coerced to string "30")
// Boolean to Number
console.log(true + 1); // 2 (true is coerced to 1)
console.log(false * 10); // 0 (false is coerced to 0)

Occurs when a developer intentionally converts a value from one data type to another, typically using built-in global functions or operators.

// String to Number
let str = "42";
let num = Number(str); // Explicit coercion
console.log(num); // 42 (number)
// Number to String
let value = 99;
let text = String(value); // Explicit coercion
console.log(text); // "99"
// Boolean to String
let isActive = true;
console.log(String(isActive)); // "true"
// Anything to Boolean
console.log(Boolean(0)); // false (explicit)
console.log(Boolean("")); // false
console.log(Boolean("hello")); // true

Logical operators allow you to combine multiple conditions or evaluate logical expressions.

Returns true only if both conditions are true.

let a = true;
let b = false;
console.log(a && b); // false
console.log(a && !b); // true

Returns true if one or more conditions are true.

let age = 18;
console.log(age > 16 || age < 12); // true
console.log(age > 20 || age < 10); // false

Reverses a boolean value.

let isActive = true;
console.log(!isActive); // false

Assignment operators are used to assign values to variables. You can perform operations while assigning values.

let x = 10; // Assign
x += 5; // Add and assign, Equivalent to x = x + 5
console.log(x); // 15
x *= 2; // Multiply and assign, Equivalent to x = x * 2
console.log(x); // 30
x -= 10; // Subtract and assign, Equivalent to x = x - 10
console.log(x); // 20
x /= 5; // Divide and assign, Equivalent to x = x / 5
console.log(x); // 4
x %= 3; // Modulus and assign , Equivalent to x = x % 3
console.log(x); // 1