🔢 Operators in JavaScript
In JavaScript, operators are symbols that perform operations on variables and values.
They are used to manipulate data, perform calculations, and make decisions in programs.
📂 Types of Operators in JavaScript
JavaScript operators can be divided into these main categories:
1️⃣ Arithmetic Operators ➕➖✖➗
Used for mathematical calculations.
Operator |
Description |
Example |
Output |
+ |
Addition |
5 + 2 |
7 |
- |
Subtraction |
5 - 2 |
3 |
* |
Multiplication |
5 * 2 |
10 |
/ |
Division |
5 / 2 |
2.5 |
% |
Modulus (remainder) |
5 % 2 |
1 |
** |
Exponentiation |
2 ** 3 |
8 |
++ |
Increment |
let a = 5; a++ |
6 |
-- |
Decrement |
let a = 5; a-- |
4 |
2️⃣ Assignment Operators 🖊
Used to assign values to variables.
Operator |
Example |
Same As |
= |
x = 5 |
x = 5 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
%= |
x %= 5 |
x = x % 5 |
**= |
x **= 3 |
x = x ** 3 |
3️⃣ Comparison Operators ⚖
Used to compare values; returns true or false.
Operator |
Description |
Example |
Output |
== |
Equal to (value only) |
5 == '5' |
true |
=== |
Strict equal (value & type) |
5 === '5' |
false |
!= |
Not equal (value only) |
5 != '5' |
false |
!== |
Strict not equal (value & type) |
5 !== '5' |
true |
> |
Greater than |
5 > 3 |
true |
< |
Less than |
5 < 3 |
false |
>= |
Greater than or equal to |
5 >= 5 |
true |
<= |
Less than or equal to |
5 <= 4 |
false |
4️⃣ Logical Operators 🧠
Used to combine conditions.
Operator |
Description |
Example |
Output |
&& |
AND – true if both |
true && false |
false |
|| |
OR – true if one |
true || false |
true |
! |
NOT – reverses value |
!true |
false |
5️⃣ String Operators 📝
JavaScript uses + for string concatenation.
let first = "Hello";
let last = "World";
console.log(first + " " + last); // Hello World
+= can also be used for appending strings.
6️⃣ Ternary Operator (Conditional) ❓
A shorthand for if...else.
let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // Adult
7️⃣ Type Operators 🏷
Used for type checking and object handling.
Operator |
Description |
Example |
Output |
typeof |
Returns type of variable |
typeof "Hello" |
"string" |
instanceof |
Checks if object is instance of class |
arr instanceof Array |
true |
8️⃣ Bitwise Operators ⚡
Operate on binary numbers.
Operator |
Description |
Example |
Binary Result |
& |
AND |
5 & 1 |
1 (0101 & 0001) |
| |
OR |
5 | 1 |
5 (0101 | 0001) |
^ |
XOR |
5 ^ 1 |
4 |
~ |
NOT |
~5 |
-6 |
<< |
Left shift |
5 << 1 |
10 |
>> |
Right shift |
5 >> 1 |
2 |
📚 Best Practices
- ✅ Use === instead of == to avoid type coercion issues
- ✅ Group logical conditions with parentheses for clarity
- ✅ Avoid unnecessary bitwise operators unless needed
- ✅ Remember string concatenation with + is different from numeric addition
📌 Summary: JavaScript operators
- 🧮 Perform calculations (Arithmetic)
- 🖋 Assign values (Assignment)
- ⚖ Compare values (Comparison)
- 🧠 Combine conditions (Logical)
- 📄 Manipulate strings (String Operators)
- ❓ Make decisions (Ternary)
- 🏷 Check types (Type Operators)
- ⚡ Work with binary values (Bitwise)
🔄 Loops in JavaScript
In JavaScript, loops are used to execute a block of code multiple times until a certain condition is met.
They help avoid writing repetitive code.
📜 Why Use Loops?
Instead of writing:
console.log(1);
console.log(2);
console.log(3);
We can write:
javascript
CopyEdit
for (let i = 1; i <= 3; i++) {
console.log(i);
}
This saves time and makes code shorter & dynamic.
📂 Types of Loops in JavaScript
1️⃣ for Loop
Runs code a specific number of times.
📌 Syntax
for (initialization; condition; increment/decrement) {
// code block
}
📌 Example
for (let i = 1; i <= 5; i++) {
console.log(i);
}
// Output: 1 2 3 4 5
2️⃣ while Loop
Runs as long as the condition is true.
📌 Syntax
while (condition) {
// code block
}
📌 Example
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
3️⃣ do...while Loop
Executes the code block at least once before checking the condition.
📌 Syntax
do {
// code block
} while (condition);
📌 Example
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
4️⃣ for...in Loop 🏷
Used to loop through object properties.
📌 Example
let person = {name: "John", age: 25, city: "Delhi"};
for (let key in person) {
console.log(key, ":", person[key]);
}
📌 Output
name : John
age : 25
city : Delhi
5️⃣ for...of Loop 📦
Used to loop through iterable objects like arrays, strings, etc.
📌 Example
let fruits = ["Apple", "Banana", "Mango"];
for (let fruit of fruits) {
console.log(fruit);
}
📌 Output
Apple
Banana
Mango
🔄 Loop Control Statements
JavaScript provides ways to control loops.
🔹 break
Stops the loop immediately.
for (let i = 1; i <= 10; i++) {
if (i === 5) break;
console.log(i);
}
// Output: 1 2 3 4
🔹 continue
Skips the current iteration and moves to the next.
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
// Output: 1 2 4 5
📚 Best Practices
- ✅ Avoid infinite loops (make sure conditions will be false at some point).
- ✅ Use for...of for arrays, and for...in for objects.
- ✅ Prefer forEach() for cleaner array iteration when the index is not needed.
📌 Summary Table of Loops in JavaScript
Loop Type |
Used For |
Runs At Least Once? |
for |
Fixed repetitions |
❌ |
while |
Unknown repetitions |
❌ |
do...while |
Unknown repetitions |
✅ |
for...in |
Object properties |
❌ |
for...of |
Iterable elements |
❌ |