JavaScript Operators with Examples
- JavaScript operators are symbols used to perform operations on variables and values.
- They are mainly used for calculations, assigning values, and checking conditions.
The most commonly used operators are:
- Arithmetic Operators
- Assignment Operators
- Logical / Comparison Operators
1) Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Meaning | Example | Output |
|---|---|---|---|
+ |
Addition | 10 + 2 |
12 |
- |
Subtraction | 10 - 2 |
8 |
* |
Multiplication | 10 * 2 |
20 |
/ |
Division | 10 / 2 |
5 |
% |
Modulus | 10 % 3 |
1 |
Example
let a = 10;
let b = 2;
console.log(a + b); // 12
console.log(a - b); // 8
console.log(a * b); // 20
console.log(a / b); // 5
console.log(a % b); // 0
Type Casting Example
console.log(10 - "2"); // 8
- Here JavaScript converts "2" (string) into number 2.
- This is called type casting / type coercion.
Increment and Decrement Operators
These operators increase or decrease a value by 1.
| Operator | Meaning |
|---|---|
++ |
Increment |
-- |
Decrement |
Increment (++)
Increases the value by 1.
let i = 5;
i++;
console.log(i); // 6
Equivalent to:
i = i + 1;
Decrement (--)
Decreases the value by 1.
let j = 10;
j--;
console.log(j); // 9
Equivalent to:
j = j - 1;
Pre and Post Increment / Decrement
Pre Increment → ++i
First increases the value, then uses it.
let i = 5;
console.log(++i); // 6
Post Increment → i++
First uses current value, then increases it.
let i = 5;
console.log(i++); // 5
console.log(i); // 6
Pre Decrement → --i
First decreases, then uses it.
let i = 5;
console.log(--i); // 4
Post Decrement → i--
First uses current value, then decreases it.
let i = 5;
console.log(i--); // 5
console.log(i); // 4
Task Example
let i = 5;
let j = 10;
let result = --i - --j - j-- + ++i;
console.log(i);
console.log(j);
console.log(result);
Output
i = 5
j = 8
result = -9
2) Assignment Operators
Assignment operators are used to assign values to variables and update them.
The basic assignment operator is =.
Basic Assignment
let x = 10;
Here, value 10 is assigned to variable x.
Shortcut Assignment Operators
| Operator | Meaning | Example |
|---|---|---|
= |
Assign | x = 10 |
+= |
Add and assign | x += 2 |
-= |
Subtract and assign | x -= 2 |
*= |
Multiply and assign | x *= 2 |
/= |
Divide and assign | x /= 2 |
%= |
Modulus and assign | x %= 2 |
Examples (same like class style)
let i = 10;
i = i + 2; // 12
i += 2; // shortcut
i = i + 40; // 52
i += 40;
i = i - 2; // 50
i -= 2;
i = i * 2; // 100
i *= 2;
i = i / 2; // 50
i /= 2;
Short Table
| Normal Form | Shortcut Form |
|---|---|
i = i + 1 |
i++ |
i = i + 2 |
i += 2 |
i = i - 2 |
i -= 2 |
i = i * 2 |
i *= 2 |
i = i / 2 |
i /= 2 |
These make the code shorter and cleaner.
3) Logical / Comparison Operators
Logical / comparison operators are used to compare values and return true or false.
These are the same examples from your class.
Equal To (==)
Checks only the value.
console.log(10 == 10); // true
console.log(10 == "10"); // true
Here "10" is a string, but JavaScript converts it into number.
This is called type coercion.
Strict Equal To (===)
Checks both value and datatype.
console.log(10 === 10); // true
console.log(10 === "10"); // false
Because one is number and one is string.
Not Equal (!=)
Checks whether values are not equal.
console.log(10 != 10); // false
console.log(10 != "10"); // false
Both values are same after conversion.
Strict Not Equal (!==)
Checks both value and datatype.
console.log(10 !== "10"); // true
Because datatype is different.
Quick Summary Table
| Operator | Meaning | Example | Output |
|---|---|---|---|
== |
Value equal | 10 == "10" |
true |
=== |
Value + datatype equal | 10 === "10" |
false |
!= |
Not equal | 10 != 10 |
false |
!== |
Strict not equal | 10 !== "10" |
true |
Conclusion
Operators are one of the most important concepts in JavaScript.
- Arithmetic operators → used for calculations
- Increment / decrement → used for increasing and decreasing values
- Assignment operators → used for updating variables
- Logical operators → used for checking conditions
Top comments (0)