Skip to main content

Command Palette

Search for a command to run...

Understanding JavaScript Operators

Updated
•4 min read•View as Markdown
Understanding JavaScript Operators

When you start learning JavaScript, one of the first things you’ll encounter is operators. They might look simple at first — symbols like +, -, ===, or &&. But these small symbols play a huge role in how programs work.

In simple terms, operators help us perform actions on values.

For example:

5 + 3

Here, + is the operator and it tells JavaScript to add the two numbers.

If you’ve ever done basic math, you already know how some operators work. But in programming, they are used for more than just calculations — we also use them to compare values, make decisions, and update variables.

Let’s go through the most common ones you’ll use in everyday JavaScript.

Arithmetic Operators

These are the operators we use for basic calculations.

You’ve probably seen them before:

  • + → addition

  • - → subtraction

  • * → multiplication

  • / → division

  • % → remainder (modulus)

Here’s a quick example:

const a = 10;
const b = 3;

console.log(a + b); 
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);

The % operator is interesting because it gives the remainder after division.

For example:

10 ÷ 3 = 3 remainder 1

So:

10 % 3 // 1

This is often used in programming for things like checking even and odd numbers.

Comparison Operators

Sometimes we don’t want to calculate numbers — we want to compare them.

Comparison operators help us do that, and the result is always either true or false.

Some common ones are:

  • > greater than

  • < less than

  • != not equal

  • == equal (loose comparison)

  • === strict equal

Example:

console.log(5 > 3);  
console.log(2 < 1);  
console.log(10 != 5);

These return boolean values (true or false) which are often used in conditions and decisions.

The Important Difference Between == and ===

This is one of the most common things beginners get confused about.

Consider this:

console.log(5 == "5");

Output:

true

Why?

Because == allows type conversion. JavaScript converts the string "5" into a number before comparing.

Now look at this:

console.log(5 === "5");

Output:

false

Here === checks both value and type.

  • 5 → number

  • "5" → string

Since the types are different, the result is false.

Because of this, most developers prefer using === instead of == to avoid unexpected behavior.

Logical Operators

Logical operators help us combine multiple conditions.

The most common ones are:

  • && (AND)

  • || (OR)

  • ! (NOT)

AND (&&)

Both conditions must be true.

const age = 20;
const hasID = true;

console.log(age > 18 && hasID);

This returns true only if both conditions are true.

OR (||)

Only one condition needs to be true.

const isWeekend = true;
const isHoliday = false;

console.log(isWeekend || isHoliday);

Since one condition is true, the result is true.

NOT (!)

This simply reverses a boolean value.

const isLoggedIn = false;

console.log(!isLoggedIn);

Output:

true

Assignment Operators

Assignment operators are used to store values in variables.

The most basic one is =.

let score = 10;

This assigns the value 10 to the variable score.

But there are also shorthand operators that make code cleaner.

Example:

let points = 10;

points += 5;
console.log(points);

This is the same as writing:

points = points + 5;

Similarly:

points -= 3;

means:

points = points - 3;

These shortcuts are very common in real-world code.

A Small Practice Exercise

Arithmetic

const x = 12;
const y = 4;

console.log(x + y);
console.log(x * y);
console.log(x % y);

Comparison

console.log(10 == "10");
console.log(10 === "10");

Logical Condition

const age = 22;
const hasTicket = true;

if (age > 18 && hasTicket) {
  console.log("Entry allowed");
}