Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators Explained

Updated
5 min read

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

For example:

  • Add two numbers

  • Check if a user is 18 or older

  • Verify if two values are equal

  • Combine conditions like age > 18 AND user is logged in

To perform these operations, JavaScript uses operators.

In this article, we’ll learn the most important operators used in everyday JavaScript programming:

  • Arithmetic operators

  • Comparison operators

  • Logical operators

  • Assignment operators

All examples are simple so you can try them directly in your browser console or Node.js.


What Are Operators?

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

Example:

let result = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

The operator tells JavaScript what action to perform.

Output:

8

Arithmetic Operators

Arithmetic operators are used for basic mathematical calculations.

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

Addition (+)

let a = 10;
let b = 5;

console.log(a + b);

Output:

15

Subtraction (-)

console.log(10 - 3);

Output:

7

Multiplication (*)

console.log(4 * 3);

Output:

12

Division (/)

console.log(10 / 2);

Output:

5

Modulus (%)

The modulus operator returns the remainder of a division.

console.log(10 % 3);

Output:

1

Because:

10 ÷ 3 = 3 remainder 1

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

Example:

console.log(4 % 2); // 0 (even)
console.log(5 % 2); // 1 (odd)

Comparison Operators

Comparison operators are used to compare two values.

They always return a boolean value:

  • true

  • false

Operator Meaning
== Equal to
=== Strict equal
!= Not equal
> Greater than
< Less than

Equal to (==)

== checks if two values are equal, but it converts types if needed.

Example:

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

Output:

true

Because JavaScript converts "5" into the number 5.


Strict Equal (===)

=== checks both value and type.

Example:

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

Output:

false

Why?

  • 5 → number

  • "5" → string

Different types, so the result is false.

Most developers prefer using === to avoid unexpected results.


Not Equal (!=)

Checks if two values are not equal.

Example:

console.log(10 != 5);

Output:

true

Greater Than (>) and Less Than (<)

console.log(10 > 5);

Output:

true

Example:

console.log(3 < 7);

Output:

true

These are often used in conditions and decision making.


Logical Operators

Logical operators are used to combine multiple conditions.

Operator Meaning
&& AND
! NOT

Logical AND (&&)

Returns true only if both conditions are true.

Example:

let age = 20;
let hasID = true;

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

Output:

true

Both conditions are true.


Logical OR (||)

Returns true if at least one condition is true.

Example:

let isWeekend = false;
let isHoliday = true;

console.log(isWeekend || isHoliday);

Output:

true

At least one condition is true.


Logical NOT (!)

The NOT operator reverses a boolean value.

Example:

let isLoggedIn = false;

console.log(!isLoggedIn);

Output:

true

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Example Meaning
= x = 5 Assign value
+= x += 3 Add and assign
-= x -= 2 Subtract and assign

Basic Assignment (=)

let score = 10;

This assigns the value 10 to score.


Addition Assignment (+=)

let score = 10;

score += 5;

console.log(score);

Output:

15

This is the same as writing:

score = score + 5

Subtraction Assignment (-=)

let score = 10;

score -= 3;

console.log(score);

Output:

7

This is the same as:

score = score - 3

Operators are a fundamental part of JavaScript because they allow us to:

  • Perform calculations

  • Compare values

  • Combine conditions

  • Update variable values

The most commonly used operators include:

  • Arithmetic operators → math operations

  • Comparison operators → compare values

  • Logical operators → combine conditions

  • Assignment operators → update variables