Operators are symbols that perform operations on values and variables.
Example:
let sum = 10 + 5;
Here:
10 and 5 → Operands+ → OperatorOutput:
15
Operators allow JavaScript to perform calculations, compare values, assign data, make decisions, and manipulate variables.
Operators allow JavaScript to:
JavaScript provides several categories of operators:
Assignment operators assign values to variables.
=)let age = 21;
Meaning:
Store 21 inside age
Example
let x = 10;
console.log(x);
Output
10
+=)let x = 10;
x += 5;
Equivalent to
x = x + 5;
Output
15
-=)let x = 10;
x -= 3;
Equivalent to
x = x - 3;
Output
7
*=)let x = 10;
x *= 2;
Output
20
/=)let x = 10;
x /= 2;
Output
5
%=)let x = 10;
x %= 3;
Output
1
| Operator | Example | Equivalent |
|---|---|---|
= |
x = 5 |
Assign value |
+= |
x += 2 |
x = x + 2 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 2 |
x = x / 2 |
%= |
x %= 2 |
x = x % 2 |
Arithmetic operators perform mathematical calculations.
+)10 + 5;
Output
15
-)10 - 5;
Output
5
*)10 * 5;
Output
50
/)10 / 5;
Output
2
%)Returns the remainder after division.
10 % 3;
Output
1
**)Raises a number to a power.
2 ** 3;
Output
8
Equivalent to
2 × 2 × 2
| Operator | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
** |
Exponentiation |
Comparison operators compare two values.
The result is always:
truefalse==)5 == "5";
Output
true
JavaScript converts the values before comparing them.
===)5 === "5";
Output
false
No type conversion occurs.
Best Practice: Always prefer
===over==.
!=)5 != 3;
Output
true
!==)5 !== "5";
Output
true
>)10 > 5;
Output
true
<)5 < 10;
Output
true
>=)10 >= 10;
Output
true
<=)5 <= 10;
Output
true
| Operator | Meaning |
|---|---|
== |
Equal |
=== |
Strict Equal |
!= |
Not Equal |
!== |
Strict Not Equal |
> |
Greater Than |
< |
Less Than |
>= |
Greater Than or Equal |
<= |
Less Than or Equal |
Logical operators combine multiple conditions.
&&)Returns true only if both conditions are true.
true && true;
Output
true
Example
age >= 18 && age <= 60;
Meaning:
Age Between 18 and 60
||)Returns true if at least one condition is true.
true || false;
Output
true
Example
role === "admin" || role === "manager";
!)Reverses a boolean value.
!true;
Output
false
Example
let isLoggedIn = false;
console.log(!isLoggedIn);
Output
true
| Operator | Meaning |
|---|---|
&& |
AND |
|| |
OR |
! |
NOT |
Bitwise operators work directly with binary numbers.
These are mainly used in low-level programming and are less common in everyday web development.
&)5 & 1;
Binary
5 = 101
1 = 001
--------
001
Output
1
|)5 | 1;
Output
5
^)5 ^ 1;
Output
4
~)~5;
Output
-6
<<)5 << 1;
Output
10
>>)10 >> 1;
Output
5
| Operator | Meaning |
|---|---|
& |
AND |
| |
OR |
^ |
XOR |
~ |
NOT |
<< |
Left Shift |
>> |
Right Shift |
Unary operators work with a single operand.
+)Converts a value into a number.
+"100";
Output
100
-)-"100";
Output
-100
++)Adds one.
let x = 5;
x++;
Output
6
--)Subtracts one.
let x = 5;
x--;
Output
4
Returns the data type.
typeof "Hello";
Output
string
Removes an object property.
const user = {
name: "Samir"
};
delete user.name;
| Operator | Meaning |
|---|---|
+ |
Convert to Number |
- |
Negative Value |
++ |
Increment |
-- |
Decrement |
typeof |
Data Type |
delete |
Remove Property |
The ternary operator is a shorter way of writing an if...else statement.
condition
? valueIfTrue
: valueIfFalse;
let age = 20;
let result =
age >= 18
? "Adult"
: "Minor";
Output
Adult
Equivalent if...else
if (age >= 18) {
result = "Adult";
} else {
result = "Minor";
}
const isLoggedIn = true;
const message =
isLoggedIn
? "Welcome"
: "Login First";
Output
Welcome
String operators combine and manipulate text.
+)"Hello" + " World";
Output
Hello World
let firstName = "Samir";
let lastName = "Niroula";
let fullName =
firstName + " " + lastName;
Output
Samir Niroula
+=)let text = "Hello";
text += " World";
Output
Hello World
Instead of
let fullName =
firstName + " " + lastName;
Use
let fullName =
`${firstName} ${lastName}`;
Template literals are cleaner and easier to read.
let age = 21;
age += 1;
let status =
age >= 18
? "Adult"
: "Minor";
console.log(status);
Output
Adult
JavaScript follows mathematical precedence rules.
Example
10 + 5 * 2;
Output
20
Because:
5 × 2 = 10
10 + 10 = 20
Parentheses change the order of evaluation.
(10 + 5) * 2;
Output
30
| Category | Operators |
|---|---|
| Assignment | =, +=, -=, *=, /=, %= |
| Arithmetic | +, -, *, /, %, ** |
| Comparison | ==, ===, !=, !==, >, <, >=, <= |
| Logical | &&, ||, ! |
| Bitwise | &, |, ^, ~, <<, >> |
| Unary | +, -, ++, --, typeof, delete |
| Conditional | ?: |
| String | +, += |
Operators allow JavaScript to perform calculations, comparisons, assignments, and logical decisions.
The most commonly used operators in modern JavaScript are:
+
-
*
/
===
!==
>
<
&&
||
++
--
?:
Remember:
Assignment → Store Values
Arithmetic → Perform Math
Comparison → Compare Values
Logical → Combine Conditions
Unary → Modify Single Values
Ternary → Short if...else
String → Join Text
Learning these operators is essential because they are used in almost every JavaScript program.