Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Simple Guide to Making Your Code "Do Things"

What Exactly is an Operator?

Updated
3 min readView as Markdown
JavaScript Operators: The Simple Guide to Making Your Code "Do Things"
A

Full-stack developer and startup founder building tech solutions in Ayodhya. Vlogger with 27k+ followers sharing my journey in technology and SaaS.

What are Operators?

In simple terms, an operator is a special symbol used to perform operations on values and variables.

For example, in 5 + 2, the + is the operator. It tells JavaScript: "Take these two numbers and add them together."

1. Arithmetic Operators (The Math Stuff)

These are the ones you already know from school. They allow you to perform basic calculations.

  • + (Addition): Adds values.

  • - (Subtraction): Subtracts values.

  • * (Multiplication): Multiplies values.

  • / (Division): Divides one value by another.

  • % (Remainder/Modulo): Gives you what is left over after division.

Console Example:

let apples = 10;
let friends = 3;

console.log(apples + 5); // 15
console.log(apples * 2); // 20
console.log(apples % friends); // 1 (Because 3 goes into 10 three times, with 1 left over)

2. Assignment Operators

These are used to set or update the value of a variable.

  • = : The basic assignment (Sets x to 5).

  • += : Adds a value to the current variable and updates it.

  • -= : Subtracts a value from the current variable and updates it.

Console Example:

let score = 10;
score += 5; // Same as: score = score + 5
console.log(score); // 15

3. Comparison Operators

We use these to compare two values. The result is always True or False.

  • > and < : Greater than and less than.

  • == : Equal to (checks the value only).

  • === : Strict equal to (checks the value AND the data type).

The Big Difference: == vs ===

This is a common trap for beginners.

  • 5 == "5" is true because JavaScript tries to be "helpful" and ignores that one is a number and one is text.

  • 5 === "5" is false because they are different types. Always use === to avoid bugs!

4. Logical Operators

These help us combine multiple conditions together.

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

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

  • ! (NOT): Flips the value (True becomes False).

truth table for logical operators, AI generated

Console Example:

let isSunny = true;
let hasFreeTime = true;

if (isSunny && hasFreeTime) {
    console.log("Go for a walk!"); 
}

Quick Summary Table

Category

Symbols

Use Case

Arithmetic

+, -, *, /, %

Doing math

Comparison

==, ===, >, <

Comparing values

Logical

&&, `

Assignment

=, +=, -=

Storing or changing values