Operators in JavaScript
In JavaScript, operators are special symbols used to perform actions on values or variables. You’ll use them every day while writing code—whether you’re doing math, checking if something is true or false, or assigning values to variables. Understanding how operators work is a basic but important step in learning JavaScript.
Arithmetic Operators
Arithmetic operators are used to do simple math.
Addition (+)
+
adds two numbers
console.log(10 + 5);
We can store values inside variables.
let a = 10;
let b = 5;
console.log(a + b); // Output: 15
Subtraction (-)
-
subtracts two numbers
let a = 10;
let b = 5;
console.log(a - b); // Output: 5
Multiplication (*)
*
multiplies two numbers
let a = 10;
let b = 5;
console.log(a * b); // Output 50
Division (/)
/
divides the first number by the second:
let a = 10;
let b = 5;
console.log(a / b); // Output: 2
Exponentiation (**)
Exponentiation (**)
refers to raising a number (the base) to the power of another number (the exponent). This operation is used to multiply a number by itself a certain number of times.
Example:
console.log(2 ** 3); // Output: 8 (2 × 2 × 2)
console.log(5 ** 2); // Output: 25 (5 × 5)
console.log(10 ** -1); // Output: 0.1 (1 ÷ 10)
You can use it with variables too:
let base = 4;
let exponent = 0.5;
console.log(base ** exponent); // Output: 2 (square root of 4)
Modulus (%)
%
gives the remainder after division:
let a = 10;
let b = 5;
console.log(a % b); // Output: 0 (no remainder)
console.log(7 % 3); // Output: 1 (because 7 divided by 3 leaves 1)
Increment (++)
In JavaScript, incrementing(++)
means increasing a number's value by one. It's commonly done using the increment operator: ++
.
Types of Increment:
- Post-increment
(x++)
The current value is used first, and then the variable is incremented.
let x = 5;
console.log(x); // Output: 5
let y = x++; // y = 5, x becomes 6
console.log(y); // Output: 5
console.log(x); // Output: 6
- Pre-increment
(++x)
The variable is incremented first, and then the new value is used.
let x = 5;
console.log(x); // Output: 5
let y = ++x; // x becomes 6, y = 6
console.log(y); // Output: 6
console.log(x); // Output: 6
Decrement (--)
In JavaScript, decrement means reducing the value of a variable by 1.
There are two types:
- Post-decrement
(x--)
The current value is used first, then decreased by 1.
let x = 5;
console.log(x); // Output: 5
let y = x--; // y = 5, x becomes 4
console.log(y); // Output: 5
console.log(x); // Output: 4
- Pre-decrement
(--x)
The value is decreased first, then used.
let x = 5;
console.log(x); // Output: 5
let y = --x; // x becomes 4, then y = 4
console.log(y); // Output: 4
console.log(x); // Output: 4
Assignment Operators
Assignment operators let you store values in variables or update them.
let x = 10; // assigns 10 to x
x += 5; // same as x = x + 5 → now x is 15
x -= 2; // x = x - 2 → now x is 13
x *= 2; // x = x * 2 → now x is 26
x /= 2; // x = x / 2 → now x is 13
x %= 4; // x = x % 4 → now x is 1
These shortcuts help you write cleaner and faster code.
Comparison Operators
Comparison operators are used to check if two values are equal or not. They return true
or false
.
Equality Operator (==)
- Equality operator
==
Checks if values are equal - It returns
true
if the two values are equal. - It returns
false
if they are not equal.
console.log(5 == "5"); // Output: true
Strict Equality Operator (===)
- Strict equality Operator
===
checks if values and types are equal - It returns
true
only if both the value and the type are exactly the same.
console.log(5 === "5"); // Output: false, (same value, different types: number vs string)
console.log(5 === 5); // Output: true, (same value, same type: number)
console.log(true === 1 ); // Output: false, (boolean vs number)
console.log(null === undefined); // Output: false, (different types)
Inequality Operator (!=)
- Inequality operator
!=
checks if values are not equal. - It returns
true
if the values are different. - It return
false
if the values are same.
console.log(5 != 1); // Output: true
console.log(5 != "5"); // Output: false
Strict inequality Operator (!==)
- Strict inequality operator
!==
checks if values or types are not equal. - It return
true
if the values are not the same typeor
The values are the same type but not equal. - It return
false
if The values are of the same type and have equal value.
console.log(5 !== "5"); // Output: true
Using strict equality operator ===
and strict inequality operator !==
is more strict and is generally recommended to avoid unexpected results in your code.
Greater than (>) Operator
- Greater than
(>)
operator Check if the left number is greater than the right number. - It returns
true
if the left number is greater than the right number. - It returns
false
if the right number is greater than the left number.
console.log(5 > 2); // Output: true, 5 (the left number) is greater than 2 (the right number)
console.log(5 > 10); // Output: false, 5 (the left number) is less than 10 (the right number)
Greater than or Equal to (>=) Operator
- Greater than or equal to
(>=)
operator checks if the left number is greater than or equal to the right number. - It returns
true
if the left number is greater than or equal to the right number. - It returns
false
if the right number is greater than the left number.
console.log(120 >= 100); // Output: true, 120 (the left number) is greater than 100 (the right number)
console.log(100 >= 100); // Output: true, 100 (the left number) equals 100 (the right number)
console.log(95 >= 100); // Output: false, 95 (the left number) is less than 100 (the right number)
Less than (<)
- Less than
(<)
check if the left number is smaller than the right number. - It returns
true
if the left number is smaller than the right number. - it returns
false
if the left number is greater than the right number.
console.log(20 < 30); // Output: true, 20 (the left number) is smaller than 30 (the right number)
console.log(220 < 100); // Output: false, 220 (the left number) is greater than 100 (the right number)
Less than or Equal to (<=)
- Less than or equal to
(<=)
checks if the left number is smaller than or equal to the right number. - It returns
true
if the left number is less than or equal to the right number. - It returns
false
if the left number is greater than the right number.
console.log(5 <= 10); // Output: true, 5 (the left number) is smaller than 10 (the right number)
console.log(10 <= 10); // Output: true, 10 (the left number) equals 10 (the right number)
console.log(20 <= 10); // Output: false, 20 (the left number) is greater than 10 (the right number)
Logical Operators
Logical operators help you combine multiple conditions.
AND (&&) Operator
- You can check more than one condition using And
(&&)
operator. - Returns
true
if all conditions are correct. - Returns
false
if one of the conditions is incorrect, because the entire expression will evaluate to false.
console.log(6 > 4 && 6 > 5); // Output: true, all conditions are true
// The first condition (6 > 4) is true because 6 is greater than 4
// The second condition (6 > 5) is true because 6 is greater than 5
console.log(6 > 4 && 5 > 5); // Output: false, there is a true condition and an incorrect condition
// The first condition (6 > 4) is true because 6 is greater than 4
// The second condition (5 < 5) is false because 5 is equal to 5
OR (||) Operator
- You can check more than one condition using Or
(||)
operator.. - Returns
true
if all conditions are true or at least one condition is true. - Returns
false
if all conditions are false.
console.log(6 > 4 || 6 > 5); // Output: true, all conditions are true
// The first condition (6 > 4) is true because 6 is greater than 4
// The second condition (6 > 5) is true because 6 is greater than 5
console.log(6 > 4 || 5 > 5); // Output: true, The first condition is true
// The first condition (6 > 4) is true because 6 is greater than 4
// The second condition (5 < 5) is false because 5 is not greater than 5
console.log(2 > 5 || 3 > 5); // Output: false, all the conditions are false
// The first condition (2 > 5) is false because 2 is less than 5
// The second condition (3 > 5) is false because 3 is less than 5
Not (!) Operator
Reverses the result. true becomes false, and false becomes true.
console.log(!true); // Output: false
console.log(!false); // Output: true
let isOnline = false;
console.log(!isOnline); // Output: true