Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
5 min read
JavaScript Operators: The Basics You Need to Know

We all are aware of +, -, =, /, %, *. Each of them has their own functionality of addition, subtraction, multiplication, division and more. In JavaScript, we have these and more. In this blog, I will be explaining operators.

What operators are

JavaScript operators are symbols used to perform operations such as calculations, comparing values, assigning values, and more.

2 + 3 = 5;  // + is an operator, adding 2 values 
x = 5;     // = is an operator, assigning 5 to x

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

Since childhood, we have been familiar with these operators. These are called arithmetic operators. Let’s see how to use them in JavaScript.

Addition (+)

const totalTime = 45 + 30;
console.log(totalTime); // 75

You can also use + to join (concatenate) strings.

const firstname = "Ami";
const lastname = "Rana";

console.log(firstname + " " + lastname); // Ami Rana

Subtraction (-)

const remainingBattery = 80 - 25;
console.log(remainingBattery); // 55

Multiplication (*)

const seatsPerRow = 6;
const rows = 12;

const totalSeats = seatsPerRow * rows;
console.log(totalSeats); // 72

Division (/)

const totalPages = 300;
const days = 5;

const pagesPerDay = totalPages / days;
console.log(pagesPerDay); // 60

Remainder (%) (The “Modulo”)

Whatever is left after division is called the remainder.

const leftoverMinutes = 95 % 60;
console.log(leftoverMinutes); // 35
const exactHours = 120 % 60;
console.log(exactHours); // 0

A very common use case is checking whether a number is odd or even.

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

As the name suggests, these operators are used to compare the values.

== (loose equality)

This compares values but performs type conversion if needed. That means if you compare a number with a string, JavaScript will convert the string to a number.

console.log(5 == "5"); // true ("5"(string) convert to 5(number))

console.log(0 == false); // true (false converts to 0)

console.log(null == undefined); // true (special rule in JS - because JavaScript treats them equally in loose equality as both of them don't have value. But you know those are not equal, right?)

=== (strict equality)

To avoid confusion, JavaScript provides ===. This compares both value and type, without conversion.

console.log(5 === "5");   // false (number vs string)
console.log(0 === false); // false (number vs boolean)
console.log(null === undefined); // false
console.log(5 === 5);     // true

It is recommended to use === to avoid unexpected bugs.

Inequality: != vs !==

!= works like == (loose inequality).

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

!== is strict inequality (no type conversion).

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

Greater than / Less than

console.log(10 > 5);   // true
console.log(10 < 5);   // false
console.log(10 >= 10); // true
console.log(5 <= 3);   // false

Real example:

const temperature = 38;
const isHotDay = temperature >= 35;

console.log(isHotDay); // true

Logical Operators (&&, ||, !)

When you want to check conditions, logical operators are used.

&& (AND) — All conditions must be true

const hasTicket = true;
const hasID = true;

const canEnter = hasTicket && hasID;
console.log(canEnter); // true

Real example:

const age = 25;
const hasLicense = true;

if (age >= 18 && hasLicense) {
  console.log("Can rent a bike");
}

|| (OR) — At least ONE condition must be true

const isWeekend = true;
const isHoliday = false;

const canRelax = isWeekend || isHoliday;
console.log(canRelax); // true

Real example:

const isSubscriber = false;
const hasPromoCode = true;

if (isSubscriber || hasPromoCode) {
  console.log("Premium content unlocked");

! (NOT) — Flips true to false (and vice versa)

const notificationsMuted = false;
const willNotify = !notificationsMuted;

console.log(willNotify); // true

Assignment Operators (=, +=, -=)

These operators store and update values.

= (Assignment)

const name = "Ami"; // value stored in variable

Add and Assign (+=)

Reassignment is happening, so let is required here.

let points = 10;
points += 5;  // Same as: points = points + 5

console.log(points); // 15

Subtract and Assign (-=)

let overs = 20;
overs -= 1;  // Same as: overs = overs - 1

console.log(overs); // 19

Other operators

let x = 10;

x *= 2;  // x = x * 2  →  20
x /= 4;  // x = x / 4  →  5
x %= 3;  // x = x % 3  →  2

Practical example:

let phonePrice = 25000;
const discount = 3000;

phonePrice -= discount;  // Subtract discount
console.log(phonePrice); // 22000

Practice Assignment

Try these in your browser console:

1. Perform arithmetic operations on two numbers

const num1 = 15; 
const num2 = 4;

2. Compare two values using both (== and ===)

const value1 = 10; 
const value2 = "10";

3. Write a small condition using logical operators

const temperature = 30; 
const hasUmbrella = false; 
const isRaining = true;

JavaScript operators might look small, but they are used everywhere, from simple calculations to decision-making in real programs. Understanding how they work will make your code cleaner and easier to write.

Don’t worry if everything doesn’t stick at once. Try the examples, experiment in the console, and you’ll start recognizing these operators naturally while coding.