Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
•2 min read•View as Markdown
JavaScript Operators: The Basics You Need to Know

What are Operators?

Think of operators as the "verbs" of JavaScript. If variables are nouns (the data), operators are the actions we perform on them. For example, in 5 + 10, the + is the operator, and 5 and 10 are the operands.

Arithmetic Operators (The Calculator)

These are used to perform standard mathematical operations.

  • + (Addition): Adds numbers or joins (concatenates) strings.

  • - (Subtraction): Subtracts one number from another.

  • * (Multiplication): Multiplies two numbers.

  • / (Division): Divides the left number by the right.

  • % (Modulo): Returns the remainder of a division. (e.g., 10 % 3 is 1).

let a = 10; 
let b = 3; 
console.log(a + b); // 13 console.log(a % b); // 1 (10 divided by 3 is 3 with a remainder of 1)
console.log(a % b); // 1 (10 divided by 3 is 3 with a remainder of 1)

Arithmetic Operators (The Calculator)

These are used to store or update values in variables.

  • =Basic Assignment = Assigns a value to a variable

  • += (Addition Assignment): Adds a value and updates the variable.

  • -= (Subtraction Assignment): Subtracts a value and updates the variable.

let points = 100;
points += 50; // Same as: points = points + 50
console.log(points); // 150

Comparison Operators (The Decision Makers)

These operators compare two values and always return a Boolean (true or false).

The Golden Rule: == vs ===

One of the most common mistakes is confusing these two:

  • == (Loose Equality): Checks if the values are the same, even if they are different types (e.g., 5 == "5" is true).

  • === (Strict Equality): Checks if both the value AND the type are exactly the same (e.g., 5 === "5" is false). Always use === to avoid bugs!

console.log(5 == "5");  // true (Loose)
console.log(5 === "5"); // false (Strict - Number vs String)
console.log(10 > 5);    // true

Logical Operators

Used to combine multiple conditions or invert a value.

  • && (Logical AND): Returns true only if both sides are true.

  • || (Logical OR): Returns true if at least one side is true.

  • ! (Logical NOT): Inverts the value (true becomes false).

let isAdult = true;
let hasTicket = false;

console.log(isAdult && hasTicket); // false
console.log(isAdult || hasTicket); // true