Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Published
3 min read
JavaScript Operators: The Basics You Need to Know

What Operators Are?

In JavaScript, operators are symbols that perform actions on values. They allow you to calculate numbers, compare values, combine conditions, or update variables.

You already use operators in everyday math. For example, when you write 5 + 3, the + symbol tells the computer to add the numbers. Programming works the same way. Operators help the program manipulate data and make decisions.

Most JavaScript programs rely heavily on operators, especially when working with calculations and conditions.

Arithmetic Operators (+, -, *, /, %)

Arithmetic operators perform basic mathematical operations. They work exactly like the math you already know.

For example:

let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1

Here is what each operator does:

+ adds numbers
- subtracts numbers
* multiplies numbers
/ divides numbers
% returns the remainder of a division

The remainder operator % is especially useful when checking things like whether a number is even or odd.

Example:

let number = 8;

console.log(number % 2); // 0 means even

Comparison Operators (==, ===, !=, >, <)

Comparison operators are used to compare two values. They return a result that is either true or false.

For example:

let a = 10;
let b = 5;

console.log(a > b);  // true
console.log(a < b);  // false
console.log(a != b); // true

The two most commonly confused operators are == and ===.

Example:

console.log(5 == "5");   // true
console.log(5 === "5");  // false

The == operator checks if the values are equal after converting types if necessary.

The === operator checks both the value and the type. Since "5" is a string and 5 is a number, they are not strictly equal.

In modern JavaScript, developers usually prefer === because it avoids unexpected type conversions.

Logical Operators (&&, ||, !)

Logical operators allow you to combine or modify conditions. They are often used inside decision-making statements like if.

The && operator means AND. Both conditions must be true.

let age = 20;
let hasID = true;

console.log(age >= 18 && hasID); // true

The || operator means OR. At least one condition must be true.

let isWeekend = true;
let isHoliday = false;

console.log(isWeekend || isHoliday); // true

The ! operator means NOT. It reverses a condition.

let loggedIn = false;

console.log(!loggedIn); // true

Logical operators are powerful because they allow multiple conditions to work together.

Assignment Operators (=, +=, -=)

Assignment operators are used to store values in variables.

The simplest one is =.

let score = 10;

This assigns the value 10 to the variable score.

JavaScript also provides shortcut assignment operators that combine assignment with arithmetic.

Example:

let score = 10;

score += 5;
console.log(score); // 15

This is the same as writing:

score = score + 5;

Similarly:

score -= 3;
console.log(score); // 12

This is equivalent to:

score = score - 3;

These shortcuts make code shorter and easier to read when updating values repeatedly.

JavaScript Operators: The Basics You Need to Know