Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

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

Imagine you are teaching a computer how to think.

Computers are extremely powerful machines, but they have one limitation: they only do exactly what you tell them to do.

If you want a computer to add numbers, compare values, or make decisions, you need a way to express those instructions.

This is where operators come into play.

Operators are the symbols that tell JavaScript what action to perform.

Think of operators like tools in a toolbox.
Each tool has a specific job.

For example:

  • A hammer drives nails.

  • A screwdriver tightens screws.

  • A wrench loosens bolts.

Similarly, JavaScript operators perform tasks such as:

  • Adding numbers

  • Comparing values

  • Checking conditions

  • Updating variables

Once you understand operators, you unlock the ability to make programs dynamic and intelligent.

Let's explore them step by step.


What Are Operators in JavaScript?

In simple terms:

Operators perform operations on values.

Those values are called operands.

Example:

let result = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

  • The result becomes 8

Without operators, programming would be impossible because we wouldn't be able to manipulate data.


Types of Operators We'll Learn

In this guide, we focus on the most commonly used operators in everyday JavaScript:

  1. Arithmetic Operators

  2. Comparison Operators

  3. Logical Operators

  4. Assignment Operators

These four categories cover most programming tasks beginners need.


1. Arithmetic Operators

Arithmetic operators are the basic math operators used to perform calculations.

If you've used a calculator before, you already understand them.

Common Arithmetic Operators

Operator Meaning Example
+ Addition 5 + 3
- Subtraction 8 - 2
* Multiplication 4 * 6
/ Division 10 / 2
% Modulus (remainder) 10 % 3

Addition Example

let a = 10;
let b = 5;

console.log(a + b);

Output

15

Subtraction Example

console.log(10 - 4);

Output

6

Multiplication Example

console.log(3 * 7);

Output

21

Division Example

console.log(20 / 5);

Output

4

Modulus Operator

The modulus operator returns the remainder after division.

console.log(10 % 3);

Output

1

This is very useful when checking things like even or odd numbers.

Example:

let number = 8;

if (number % 2 === 0) {
  console.log("Even number");
}

2. Comparison Operators

Comparison operators compare two values and return either:

true
or
false

These are extremely important because they allow programs to make decisions.


Common Comparison Operators

Operator Meaning
== Equal value
=== Equal value and type
!= Not equal
> Greater than
< Less than

Example

console.log(10 > 5);

Output

true

The Important Difference Between and =

This is one of the most important concepts in JavaScript.

Example:

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

Output

true

Why?

Because == converts types automatically.

But:

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

Output

false

Because:

  • 5 is a number

  • "5" is a string

=== checks both value AND type.

Most professional developers prefer ===.


3. Logical Operators

Logical operators are used to combine multiple conditions.

Think of them as decision-making tools.


Logical Operators

Operator Meaning
&& AND
! NOT

AND Operator (&&)

Both conditions must be true.

Example:

let age = 20;
let hasID = true;

if (age > 18 && hasID) {
  console.log("You can enter");
}

OR Operator (||)

At least one condition must be true.

let isAdmin = false;
let isEditor = true;

if (isAdmin || isEditor) {
  console.log("Access granted");
}

NOT Operator (!)

This reverses the result.

let isLoggedIn = false;

console.log(!isLoggedIn);

Output

true

Logical Operator Truth Table

Example truth table:

| A | B | A && B | A || B | | --- | --- | --- | --- | | true | true | true | true | | true | false | false | true | | false | true | false | true | | false | false | false | false |


4. Assignment Operators

Assignment operators assign values to variables.

The most common one is:

=

Example:

let score = 100;

Other Assignment Operators

Operator Example Meaning
= x = 5 assign value
+= x += 2 x = x + 2
-= x -= 3 x = x - 3

Example

let count = 10;

count += 5;

console.log(count);

Output

15

Real-World Example

Let's imagine you are building a shopping cart system.

let price = 50;
let quantity = 3;

let total = price * quantity;

if (total > 100) {
  console.log("You get free shipping!");
}

Here we used:

  • arithmetic operator *

  • comparison operator >

  • assignment operator =

Operators help the program calculate and make decisions.

JavaScript operators might seem small, but they are the foundation of programming logic.

Every program you write will use operators constantly.

They help you:

  • perform calculations

  • compare values

  • combine conditions

  • update variables

Mastering operators means you are taking the first real step toward thinking like a programmer.

And once you understand these basics, you are ready to move on to loops, functions, and real applications.