Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
8 min readView as Markdown
JavaScript Operators: The Basics You Need to Know

JavaScript becomes much more useful once you start doing things with values. You might want to add two numbers, compare two inputs, or check whether a condition is true before showing some output on the screen.

To make all of that happen, JavaScript uses operators.

Operators are symbols that tell JavaScript to perform actions on values. They help us do calculations, compare data, assign values to variables, and build conditions in our programs.

In this article, we’ll look at the basic JavaScript operators in a simple and beginner-friendly way, with examples you can easily try in the console.


What Are Operators in JavaScript?

Operators are special symbols that tell JavaScript to perform an operation on one or more values.

For example:

let sum = 5 + 3;

Here, the + symbol is an operator. It tells JavaScript to add 5 and 3.

So in simple words:

Operators are tools that help us work with data.

You’ll use them all the time while writing JavaScript.


1. Arithmetic Operators

Arithmetic operators are used for basic math operations.

These are the most common ones:

  • + → Addition

  • - → Subtraction

  • * → Multiplication

  • / → Division

  • % → Modulus (remainder)

Let’s see them in action.

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.3333333333333335
console.log(a % b); // 1

Understanding % (Modulus)

The modulus operator gives the remainder after division.

console.log(10 % 3); // 1

Because:

  • 3 goes into 10 three times

  • remainder is 1

This is very useful when checking whether a number is even or odd.

let number = 8;

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

If the remainder is 0, the number is even.


A Small Arithmetic Example

let num1 = 20;
let num2 = 5;

console.log("Addition:", num1 + num2);
console.log("Subtraction:", num1 - num2);
console.log("Multiplication:", num1 * num2);
console.log("Division:", num1 / num2);
console.log("Remainder:", num1 % num2);

Output :

Addition: 25
Subtraction: 15
Multiplication: 100
Division: 4
Remainder: 0

2. Comparison Operators

Comparison operators are used to compare two values.

They always return either:

  • true

  • false

These are some important comparison operators:

  • == → Equal to

  • === → Strict equal to

  • != → Not equal to

  • > → Greater than

  • < → Less than

Example:

let x = 10;
let y = 5;

console.log(x > y);  // true
console.log(x < y);  // false
console.log(x == y); // false
console.log(x != y); // true

The Important Difference Between == and ===

This is one of the most important things beginners should understand.

== checks value only

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

Why is this true?

Because == compares only the value, and JavaScript tries to convert one type into another automatically.


=== checks value and type

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

This is false because:

  • 5 is a number

  • "5" is a string

Even though they look similar, their types are different.

So:

  • == → loose equality

  • === → strict equality

Which one should you prefer?

In most cases, beginners and professionals are both encouraged to use:

===

because it avoids confusion and gives more accurate results.


More Comparison Examples

console.log(10 == "10");   // true
console.log(10 === "10");  // false
console.log(7 != 3);       // true
console.log(8 > 2);        // true
console.log(4 < 1);        // false

These examples are simple, but they form the base of decision-making in JavaScript.


3. Logical Operators

Logical operators are used when you want to combine conditions.

The main logical operators are:

  • && → AND

  • || → OR

  • ! → NOT

These operators are mostly used inside if statements and conditions.


&& (AND)

This returns true only if both conditions are true.

let age = 20;
let hasID = true;

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

This means:

  • age is 18 or above

  • and person has an ID

Since both are true, the result is true.


|| (OR)

This returns true if at least one condition is true.

let isWeekend = false;
let isHoliday = true;

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

Even though isWeekend is false, isHoliday is true, so the final result is true.


! (NOT)

This reverses the result.

  • true becomes false

  • false becomes true

let isLoggedIn = false;

console.log(!isLoggedIn); // true

Since isLoggedIn was false, using ! changes it to true.


Small Logical Operator Example

let marks = 75;
let attendance = 80;

console.log(marks >= 40 && attendance >= 75); // true

This checks whether a student has:

  • passed the exam

  • and has enough attendance

Both conditions are true, so the result is true.


4. Assignment Operators

Assignment operators are used to assign values to variables.

The most basic one is:

  • = → Assign value

Example:

let score = 50;

This means the value 50 is assigned to score.

But JavaScript also gives us shorthand assignment operators.

  • +=

  • -=

These help update a variable quickly.


+= Operator

let points = 10;
points += 5;

console.log(points); // 15

This is the same as writing:

let points = 10;
points = points + 5;

console.log(points); // 15

-= Operator

let balance = 20;
balance -= 7;

console.log(balance); // 13

This is the same as:

let balance = 20;
balance = balance - 7;

console.log(balance); // 13

These shortcuts make your code cleaner and easier to read.


Everyday Example Using Multiple Operators

Let’s combine arithmetic, comparison, logical, and assignment operators in one simple example.

let num1 = 12;
let num2 = 4;

let result = num1 + num2;
console.log("Result:", result); // 16

console.log(num1 == num2);  // false
console.log(num1 === num2); // false

let isGreater = num1 > num2;
let isEqual = num1 === num2;

console.log(isGreater && !isEqual); // true

result += 10;
console.log("Updated Result:", result); // 26

This is the kind of small practice code that helps you understand operators much better.


Practice Task for Beginners

Here’s a simple exercise idea based on this topic.

1. Perform arithmetic operations on two numbers

let a = 15;
let b = 4;

console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Modulus:", a % b);

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

let value1 = 10;
let value2 = "10";

console.log(value1 == value2);  // true
console.log(value1 === value2); // false

3. Write a small condition using logical operators

let age = 19;
let hasTicket = true;

if (age >= 18 && hasTicket) {
  console.log("You can enter.");
} else {
  console.log("You cannot enter.");
}

These examples are simple enough for beginners and useful enough to build confidence.


Why Operators Matter

Operators may look like tiny symbols, but they do a lot of important work in JavaScript.

Without operators, you wouldn’t be able to:

  • add or subtract values

  • compare input

  • make decisions in conditions

  • update variables easily

They are one of the basic building blocks of programming.

Once you understand operators well, writing JavaScript starts to feel much more natural.


Final Thoughts

When I first started learning JavaScript, operators looked very small and easy to ignore. But after writing even a few programs, it became obvious that they are everywhere.

Whether you’re doing simple math, checking if two things are equal, or building conditions in an if statement, operators are always involved.

So don’t rush through this topic.

Practice each type of operator with small examples in the console. Try changing values and predicting the output before running the code. That one habit alone will make you much better at JavaScript.

The more you use operators, the more comfortable they become.