JavaScript Operators
What are operators?
Operators are symbols that are used to perform operations on values and variables or operands. Operators are the building blocks of JavaScript expressions and can manipulate data in various ways.
The different types of operators are -
Arithmatic operator
Arithmatic operator perform mathematical calculations like addition, subtraction, multiplication, division, etc. It always returns a numeric value.
Addition (+) Operator - it takes two numeric operands and returns their sum. For a string, it concatenates them.
Subtraction (-) Operator - it takes two numeric operands and subtracts the second number from the first.
Multiplication (*) Operator - it multiplies two numeric operands.
Division (/) Operator - it provides the quotient of its operands by dividing two operands.
Modulus (%) Operator - After the dividend is divided by a divisor, it returns a remainder.
const sum = 5+4;
const sub = 9-6;
const multi = 5*5;
const divi = 10/5;
const mod = 20/8;
const expo = 3 ** 2.5;
console.log(sum,sub,multi,divi,mod,expo)
//increment operator
const a = 8;
a++;
//Decrement operator
const b = 7;
b--;
console.log(a,b)
| Operator | Usage | Result |
|---|---|---|
| + (Addition) | a + b | Add two numbers and also concatenate strings. |
| - (Subtraction) | a - b | It returns the difference of the operands. |
| * (Multiplication | a * b | It returns the product of the operands. |
| / (Division) | a / b | It returns the quotient of the operands. |
| % (Modulus) | a % b | It returns the remainder of the operands. |
| ++ (Increment) | a ++ | It increases the value by adding 1. |
| -- (Decrement) | b -- | It decreases the value by 1. |
| ** (Exponentiation) | a ** b | It raises the left operand to the power of the right. |
Comparison operators
Comparison operators are useful in decision-making and always return true or false.
- Equality operator (==) - It is used to check the equality of two operands. Returns a boolean value.
// Illustration of (==) operator
let x = 8;
let y = '5';
// Checking of operands
console.log(x == 8);
console.log(y == 5);
console.log(x == y);
console.log('\n');
// Check against special values
console.log(NaN == NaN);
console.log(0 == false);
console.log(0 == null);
- Strict equality Operator (===) - It is used to check the equality and the type of the two operands. Returns a boolean value.
// Illustration of (===) operator
let x = 7;
let y = '3';
// Checking of operands
console.log(x === 7);
console.log(y === '3');
console.log(x === y);
console.log('\n');
// Check against special values
console.log(NaN === NaN);
console.log(0 === false);
console.log(0 === null);
- Inequality Operator (!=) - It is used to compare the inequality of two operands. Returns a boolean value.
// Illustration of (!=) operator
let x = 6;
let y = '5';
// Checking of operands
console.log(x != 9);
console.log(y != '5');
console.log(x != y);
console.log('\n');
// Check against special values
console.log(0 != false);
console.log(0 != null);
console.log(NaN != NaN);
- Strict inequality Operator (!==) - It is used to compare the inequality and types of two operands. Returns a boolean value.
// Illustration of (!==) operator
let x = 6;
let y = '5';
// Checking of operands
console.log(x !== 9);
console.log(y !== '5');
console.log(x !== y);
console.log('\n');
// Check against special values
console.log(0 !== false);
console.log(0 !== null);
console.log(NaN !== NaN);
- Greater than Operator (>) - it checks whether the left-side value is greater than the right-side value. Returns a boolean value.
// Illustration of (>) operator
let x = 14;
let y = '5';
// Checking of operands
console.log(x > 9);
console.log(y > '5');
console.log(y > 0);
- Less than Operator (<) - it checks whether the left-side value is less than the right-side value. Returns a boolean value.
// Illustration of (<) operator
let x = 6;
let y = '5';
// Checking of operands
console.log(x < 9);
console.log(y < '5');
console.log(y < 0);
| Operator | Usage | Result |
|---|---|---|
| Equality operator (==) | a == b | Compares the equality of two values. |
| Strict equality Operator (===) | a === b | Compares both the value and type of the operands. |
| Inequality Operator (!=) | a != b | Compares the inequality of two operators. |
| Strict inequality Operator (!==) | a !== b | Compares inequality with type. |
| Greater than Operator (>) | a > b | Checks if the left-side value is greater than the right-side value. |
| Less than Operator (<) | a < b | Checks if the left-side value is less than the right-side value. |
Difference between== and ===
| Equality ( == ) | Strict Equality ( === ) |
|---|---|
| It compares only values. | It compares only values and types as well. |
| It is used when we want JavaScript to convert the types for us. | It is used when we want to ensure both values and types are identical. |
| "6" == 6 returns true (because it converts "6" to 6). | "6" == 6 returns false (because "6" is a string and 6 is a number). |
Logical operators
Logical operators are helpful to handle multiple conditions. They are used in if statements, loops, and conditions; they also help control program flow by evaluating whether one or more conditions are true or false.
Logical AND ( && ) Operator - The logical AND (&&) operator checks if both operands are true it returns true. If any one or both operands are false, the result is false.
//Logical AND ( && ) const age = 21; const hasAdharCard = true; if(age >= 18 && hasAdharCard){ console.log("Allowed") }else{ console.log("Not Allowed") }
Truth Table
| Operand A | Operand B | A && B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
- Logical OR ( || ) Operator - The logical OR (||) operator checks whether at least one of the operands is true. If both operands are false, the result is false.
//Logical AND ( || )
const age = 21;
const isVerified = false;
if(age >= 18 || isVerified){
console.log("Allowed")
}else{
console.log("Not Allowed")
}
Truth Table
| Operand A | Operand B | A || B | | --- | --- | --- | | True | True | True | | True | False | True | | False | True | True | | False | False | False |
- Logical NOT ( ! ) Operator - The logical NOT (!) operator checks if the operand is true; it returns false. If the operand is false, it returns true.
let isAllowed = true;
console.log(!isAllowed);
let isLoggedIn = false;
if (!isLoggedIn) {
console.log("Log in!");
} else {
console.log("Welcome back!");
}
Truth Table
| Operand A | !A |
|---|---|
| True | False |
| False | True |
Assignment operators
Assignment operators are used to assign values to variables in JavaScript.
- Addition Assignment Operator(+=) - it adds the value to the right operand to a variable and assigns the result to the variable. Addition or concatenation is possible.
let a = 8;
const b = 3;
console.log(a += b );
- Subtraction Assignment Operator(-=) - it subtracts the value of the right operand from a variable and assigns the result to the variable.
let a = 8;
console.log(a -= 2);



