JavaScript Operators You Must Know

Operators are fundamental building blocks in JavaScript. They allow you to perform operations on variables and values, such as arithmetic, comparisons, logical reasoning, and more. This guide covers the most essential JavaScript operators to help you write efficient and readable code.


1. Arithmetic Operators

Arithmetic operators perform mathematical calculations:

let a = 10;
let b = 5;

console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0 (remainder)
console.log(a ** 2); // 100 (exponentiation)
  • Use them for all common calculations, from simple addition to exponentiation.

2. Assignment Operators

Assignment operators assign values to variables. They can also combine operations:

let x = 10;

x += 5;  // x = x + 5
x -= 3;  // x = x - 3
x *= 2;  // x = x * 2
x /= 4;  // x = x / 4
x %= 3;  // x = x % 3

console.log(x);
  • Makes code shorter and cleaner when updating variable values.

3. Comparison Operators

Comparison operators compare values and return true or false:

let a = 10;
let b = '10';

console.log(a == b);  // true (value equality)
console.log(a === b); // false (value + type equality)
console.log(a != b);  // false
console.log(a !== b); // true
console.log(a > 5);   // true
console.log(a < 20);  // true
console.log(a >= 10); // true
console.log(a <= 8);  // false
  • Use === and !== for strict comparisons to avoid unexpected type coercion.

4. Logical Operators

Logical operators are used to combine or invert boolean expressions:

let x = true;
let y = false;

console.log(x && y); // false (AND)
console.log(x || y); // true  (OR)
console.log(!x);     // false (NOT)
  • Essential for conditionals and complex boolean logic.

5. String Operators

The + operator can also concatenate strings:

let firstName = 'John';
let lastName = 'Doe';

let fullName = firstName + ' ' + lastName;
console.log(fullName); // John Doe
  • Combine strings efficiently in code.

6. Ternary Operator

The ternary operator provides a shorthand for if-else:

let age = 18;
let status = age >= 18 ? 'Adult' : 'Minor';
console.log(status); // Adult
  • Syntax: condition ? valueIfTrue : valueIfFalse
  • Makes simple conditional assignments concise.

7. Type Operators

Check data types and object instances using:

console.log(typeof 42);          // number
console.log(typeof 'Hello');     // string
console.log(typeof true);        // boolean
console.log(typeof {});          // object
console.log(Array.isArray([]));  // true

let date = new Date();
console.log(date instanceof Date); // true
  • Useful for type checking and validation.

8. Increment and Decrement Operators

Quickly increase or decrease numeric values:

let count = 5;

count++;  // 6 (post-increment)
++count;  // 7 (pre-increment)
count--;  // 6 (post-decrement)
--count;  // 5 (pre-decrement)

console.log(count);
  • Pre- vs. post- increment affects when the value is updated in expressions.

9. Spread and Rest Operators

The ... operator is versatile:

// Spread
const arr1 = [1, 2];
const arr2 = [...arr1, 3, 4];
console.log(arr2); // [1, 2, 3, 4]

// Rest
function sum(...numbers) {
  return numbers.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
  • Simplifies array and function operations.

10. Wrapping Up

JavaScript operators are essential for manipulating data, performing calculations, and writing concise expressions. By mastering arithmetic, comparison, logical, assignment, and other key operators, you can write more efficient, readable, and powerful code.


Next Step: Explore operator precedence and short-circuiting to write even cleaner and more optimized JavaScript expressions.