DEV Community

Cover image for Understanding JavaScript Operators
Harman Panwar
Harman Panwar

Posted on

Understanding JavaScript Operators

When learning JavaScript, one of the first things you encounter is operators.
Operators allow us to perform calculations, compare values, and build logical conditions in our programs.

In this article, we'll explore the most commonly used JavaScript operators with simple examples.

What Are Operators?

Operators are symbols used to perform operations on values or variables.

For example:

let a = 10;
let b = 5;

let result = a + b;
console.log(result);

Enter fullscreen mode Exit fullscreen mode
Output: 15
Enter fullscreen mode Exit fullscreen mode

Here, the + symbol is an operator that adds two numbers.

Operators are used constantly in JavaScript programs for tasks like:

  • Performing mathematical calculations
  • Comparing values
  • Building conditions
  • Assigning values to variables Let's look at the most common types of operators.

Arithmetic Operators

Arithmetic operators perform basic mathematical calculations.

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

Example

let a = 10;
let b = 3;

console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Remainder:", a % b);
Enter fullscreen mode Exit fullscreen mode

Output:

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.333
Remainder: 1

Enter fullscreen mode Exit fullscreen mode

The % operator returns the remainder after division.

Comparison Operators

Comparison operators compare two values and return either true or false.

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

Example

console.log(5 > 3);  // true
console.log(5 < 3);  // false
console.log(5 != 3); // true
Enter fullscreen mode Exit fullscreen mode

These operators are mainly used inside conditions.

The Difference Between == and ===

This is one of the most important concepts for JavaScript beginners.

Loose Equality (==)

== compares values after converting data types.

console.log(5 == "5");
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

JavaScript converts "5" (string) into 5 (number).

Strict Equality (===)

=== compares both value and data type.

console.log(5 === "5");
Enter fullscreen mode Exit fullscreen mode

Output:

false
Enter fullscreen mode Exit fullscreen mode

Because:

5 → number

"5" → string

Their types are different, so the result is false.

Because of this behavior, developers usually prefer using ===.

Logical Operators

Logical operators help combine multiple conditions.

 Operator  Description 

   &&      AND   

   ||      OR  

   !       NOT   

Enter fullscreen mode Exit fullscreen mode

AND Operator (&&)

Both conditions must be true.

let age = 20;

console.log(age > 18 && age < 30);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

OR Operator (||)

Only one condition needs to be true.

let marks = 40;

console.log(marks > 90 || marks > 35);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

NOT Operator (!)

Reverses the result.

console.log(!(5 > 3));
Enter fullscreen mode Exit fullscreen mode

Output:

false
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

Assignment operators are used to assign values to variables.

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

Example

let x = 10;

x += 5;
console.log(x);

x -= 3;
console.log(x);
Enter fullscreen mode Exit fullscreen mode

Output:

15
12
Enter fullscreen mode Exit fullscreen mode

Here:

x += 5
Enter fullscreen mode Exit fullscreen mode

is the same as:

x = x + 5

Enter fullscreen mode Exit fullscreen mode

Practice Assignment

Try running these examples in the browser console or Node.js.

1️⃣ Perform Arithmetic Operations

let a = 12;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
Enter fullscreen mode Exit fullscreen mode

2️⃣ Compare Values

let num = 10;
let str = "10";

console.log(num == str);
console.log(num === str);
Enter fullscreen mode Exit fullscreen mode

3️⃣ Logical Condition Example

let age = 22;

if (age > 18 && age < 30) {
    console.log("You are a young adult");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)