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);
Output: 15
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);
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.333
Remainder: 1
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
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");
Output:
true
JavaScript converts "5" (string) into 5 (number).
Strict Equality (===)
=== compares both value and data type.
console.log(5 === "5");
Output:
false
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
AND Operator (&&)
Both conditions must be true.
let age = 20;
console.log(age > 18 && age < 30);
Output:
true
OR Operator (||)
Only one condition needs to be true.
let marks = 40;
console.log(marks > 90 || marks > 35);
Output:
true
NOT Operator (!)
Reverses the result.
console.log(!(5 > 3));
Output:
false
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);
Output:
15
12
Here:
x += 5
is the same as:
x = x + 5
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);
2️⃣ Compare Values
let num = 10;
let str = "10";
console.log(num == str);
console.log(num === str);
3️⃣ Logical Condition Example
let age = 22;
if (age > 18 && age < 30) {
console.log("You are a young adult");
}
Top comments (0)