Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

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

When writing JavaScript programs, we often need to perform calculations, compare values, or make decisions.

This is where operators come in.

Operators are symbols that tell JavaScript to perform an operation on values or variables.

For example:

let result = 5 + 3;

Here:

  • 5 and 3 are values

  • + is the operator

  • 8 is the result

JavaScript provides several types of operators, but in this article we'll focus on the most important beginner ones:

  • Arithmetic Operators

  • Comparison Operators

  • Logical Operators

  • Assignment Operators

These are the operators you’ll use almost every day in JavaScript.

What are Operators ?

An operator is a symbol used to perform an operation or action on values.

Example:

let total = 10 + 5;

JavaScript reads this as:

Take 10 and 5, add them together, and store the result in total

Operators help us to Perform calculations, Compare values, Assign data to variables, Create logical conditions.

Now let's explore each category.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

These are the most basic operators you'll use.

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

Addition (+)

Adds two numbers.

let result = 5 + 3;

console.log(result);

Output:

8

Subtraction (-)

Subtracts one number from another.

let result = 10 - 4;

console.log(result);

Output:

6

Multiplication (*)

Multiplies numbers.

let result = 6 * 3;

console.log(result);

Output:

18

Division (/)

Divides one number by another.

let result = 20 / 5;

console.log(result);

Output:

4

Modulus (%)

The modulus operator gives the remainder after division.

let result = 10 % 3;

console.log(result);

Output:

1

Because:

10 ÷ 3 = 3 remainder 1

This operator is often used to check even and odd numbers.

Example:

console.log(4 % 2);

Output:

0

If the remainder is 0, the number is even.

Comparison Operators

Comparison operators are used to compare two values.

They return either:

true
or
false
Operator Meaning
== Equal (loose comparison)
=== Strict equal
!= Not equal
> Greater than
< Less than

Equal To (==)

Checks if two values are equal after type conversion.

Example:

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

Output:

true

Why?

Because JavaScript converts "5" (string) into 5 (number) before comparing.

Strict Equal (===)

Checks both value and type.

Example:

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

Output:

false

Here:

  • 5 is a number

  • "5" is a string

Since the types are different, the result is false.

Difference Between == and ===

It is very important for to understand the difference between this two operators.

Expression Result Reason
5 == "5" true value matches
5 === "5" false type different

Most developers recommend using === because it avoids unexpected results.

Not Equal (!=)

Checks if values are not equal.

console.log(5 != 3);

Output:

true

Because 5 is not equal to 3.

Greater Than (>)

console.log(10 > 5);

Output:

true

Less Than (<)

console.log(3 < 8);

Output:

true

These operators are often used inside conditions and loops.

Logical Operators

Logical operators help us combine multiple conditions.

Operator Meaning
&& AND
! NOT

AND (&&)

The AND operator returns true only if both conditions are true.

Example:

let age = 20;

console.log(age > 18 && age < 30);

Output:

true

Because:

20 > 18 → true
20 < 30 → true

Both are true.

OR (||)

The OR operator returns true if at least one condition is true.

Example:

let age = 16;

console.log(age < 18 || age > 60);

Output:

true

Because:

16 < 18 → true

Only one condition needs to be true.

NOT (!)

The NOT operator reverses a boolean value.

Example:

let isLoggedIn = true;

console.log(!isLoggedIn);

Output:

false

So ! simply flips the result.

Assignment Operators

Assignment operators are used to assign values to variables.

The most common one is:

=

Example:

let number = 10;

Here, the value 10 is assigned to number.

+= Operator

Adds a value and assigns the result.

Example:

let score = 10;

score += 5;

console.log(score);

Output:

15

This is the same as:

score = score + 5;

-= Operator

Subtracts a value and assigns the result.

Example:

let score = 10;

score -= 3;

console.log(score);

Output:

7

Equivalent to:

score = score - 3;

These shorthand operators help keep code shorter and cleaner.

Summary

Category Operators
Arithmetic +, -, *, /, %
Comparison ==, ===, !=, >, <
Logical &&,
Assignment =, +=, -=

These operators form the foundation of JavaScript programming.

Operators in JavaScript