Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators Explained

Updated
•4 min read•View as Markdown
JavaScript Operators Explained

When you start learning JavaScript, one of the most important concepts is operators.
Operators help us perform operations on values and variables.

Think of operators like symbols that tell JavaScript what action to perform.

For example:

let result = 5 + 3;
console.log(result);//8

Here + is an operator that adds two numbers.

Let's explore the most commonly used operators in JavaScript.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical calculations.

Common Arithmetic Operators

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)

Example

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

Explanation

The modulus % operator returns the remainder.

Example:

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

Because 3 goes into 10 three times (9) and 1 is left.

2. Comparison Operators

Comparison operators are used to compare two values.

They return a boolean value:

  • true

  • false

Common Comparison Operators

Operator Meaning
== Equal (value only)
=== Strict equal (value + type)
!= Not equal
> Greater than
< Less than

Example

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

Difference Between == and ===

This is one of the most important JavaScript interview questions.

Example

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

Because == converts the string into a number automatically.

Using Strict Equality

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

Because: number !== string

Rule for Beginners

Use === most of the time because it checks both:

  • value

  • data type

3. Logical Operators

Logical operators are used when working with conditions.

Common Logical Operators

Operator Meaning
&& AND
! NOT

1.AND Operator &&

Returns true only if both conditions are true.

let age = 20;

console.log(age > 18 && age < 30);//true  -> because Both conditions are true

2.OR Operator ||

Returns true if at least one condition is true.

let marks = 35;

console.log(marks > 40 || marks === 35);// true

3.NOT Operator !

It reverses the value.

console.log(!true);//false

4. Assignment Operators

Assignment operators are used to assign values to variables.

Basic Assignment

let x = 10;

Here = assigns the value 10 to x.

Other Assignment Operators

Operator Meaning
= Assign
+= Add and assign
-= Subtract and assign

Example

let score = 10;

score += 5;
console.log(score);//15

Another example:

let points = 20;

points -= 5;
console.log(points);//15

Complete Example (Everyday Usage)

let price = 100;
let discount = 20;

price -= discount;

console.log("Final price:", price);

let age = 22;

if (age > 18 && age < 30) {
    console.log("Eligible");
}
//Final price: 80
//Eligible

Final Thoughts

Operators are fundamental building blocks of JavaScript.

In daily coding you will frequently use:

  • Arithmetic operators for calculations

  • Comparison operators for conditions

  • Logical operators for decision making

  • Assignment operators for updating variables

Once you master these basics, writing JavaScript logic becomes much easier.