JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Operators

2 April 2025 | Category:

JavaScript operators are symbols that perform operations on variables and values. Operators are used to manipulate data and perform calculations.


1️⃣ Types of JavaScript Operators

JavaScript has several types of operators:

TypeExampleDescription
Arithmetic+, -, *, /, %, **Perform mathematical operations
Assignment=, +=, -=, *=, /=, %=, **=Assign values to variables
Comparison==, ===, !=, !==, >, <, >=, <=Compare values
Logical&&, `
Bitwise&, `, ^, ~, <<, >>, >>>`
Ternarycondition ? trueValue : falseValueShorter if-else statement
Type Operatorstypeof, instanceofCheck variable type

2️⃣ Arithmetic Operators 🧮

Used for basic mathematical calculations.

OperatorExampleDescription
+10 + 5Addition
-10 - 5Subtraction
*10 * 5Multiplication
/10 / 5Division
%10 % 3Modulus (Remainder)
**2 ** 3Exponentiation (Power)

🔹 Example:

let a = 10, b = 5;
console.log(a + b);  // Output: 15
console.log(a % b);  // Output: 0
console.log(2 ** 3);  // Output: 8 (2^3)

3️⃣ Assignment Operators 🎯

Used to assign values to variables.

OperatorExampleEquivalent to
=x = 10Assign 10 to x
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 3x = x % 3
**=x **= 2x = x ** 2

🔹 Example:

let x = 10;
x += 5;  // x = x + 5
console.log(x);  // Output: 15

4️⃣ Comparison Operators 🔄

Used to compare two values.

OperatorExampleDescription
==5 == "5"Equal value (Type conversion allowed)
===5 === "5"Equal value & type (Strict comparison)
!=5 != 3Not equal
!==5 !== "5"Not equal (Strict comparison)
>10 > 5Greater than
<10 < 5Less than
>=10 >= 5Greater than or equal to
<=10 <= 5Less than or equal to

🔹 Example:

console.log(10 > 5);  // Output: true
console.log(5 == "5");  // Output: true (Loose comparison)
console.log(5 === "5");  // Output: false (Strict comparison)

5️⃣ Logical Operators ⚡

Used for logical operations.

OperatorExampleDescription
&&true && falseLogical AND (Both must be true)
``
!!trueLogical NOT (Reverses condition)

🔹 Example:

console.log(true && false);  // Output: false
console.log(true || false);  // Output: true
console.log(!true);  // Output: false

6️⃣ Bitwise Operators 🔢

Work on binary numbers.

OperatorExampleDescription
&5 & 1AND
```5
^5 ^ 1XOR
~~5NOT
<<5 << 1Left shift
>>5 >> 1Right shift

🔹 Example:

console.log(5 & 1);  // Output: 1 (0101 & 0001 = 0001)
console.log(5 | 1);  // Output: 5 (0101 | 0001 = 0101)

7️⃣ Ternary Operator (Shorter If-Else) 🔥

condition ? trueValue : falseValue

🔹 Example:

let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status);  // Output: Adult

8️⃣ Type Operators 🛠️

Used to check types of variables.

OperatorExampleDescription
typeoftypeof 42Returns "number"
instanceofobj instanceof ObjectChecks object type

🔹 Example:

console.log(typeof "Hello");  // Output: string
console.log(typeof 42);  // Output: number

let arr = [1, 2, 3];
console.log(arr instanceof Array);  // Output: true

🚀 Summary

✔️ Arithmetic Operators (+, -, *, /, %, **) perform calculations.
✔️ Assignment Operators (=, +=, -=, etc.) assign values.
✔️ Comparison Operators (==, ===, !=, >, <, etc.) compare values.
✔️ Logical Operators (&&, ||, !) work with conditions.
✔️ Bitwise Operators (&, |, ^, ~, <<, >>) manipulate binary numbers.
✔️ Ternary Operator (condition ? true : false) is a shorter if-else.
✔️ Type Operators (typeof, instanceof) check types of variables.