JavaScript Operators Tutorial

📌 Introduction

Operators in JavaScript are special symbols or keywords that perform operations on values and variables. They help us do calculations, comparisons, logic checks, and more. Think of operators as the tools 🧰 you use to work with data.

🧠 Types of JavaScript Operators

JavaScript operators can be grouped into several categories:

  • Arithmetic Operators
  • ⚖️ Comparison Operators
  • 🧩 Logical Operators
  • 📌 Assignment Operators
  • 🔢 Bitwise Operators
  • Other Operators (typeof, ternary, etc.)

➕ Arithmetic Operators

OperatorMeaningExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
%Modulus (Remainder)5 % 21
**Exponentiation5 ** 225

Arithmetic Operators Example

let a = 10, b = 3;
console.log(a + b); // 13
console.log(a % b); // 1
console.log(a ** b); // 1000

⚖️ Comparison Operators

Comparison operators compare two values and return true or false.

OperatorMeaningExampleResult
==Equal (loose)5 == "5"true
===Strict equal5 === "5"false
!=Not equal5 != 4true
!==Strict not equal5 !== "5"true
>Greater than7 > 5true
<Less than7 < 5false

🧩 Logical Operators

Logical operators combine conditions.

  • && ➝ Logical AND (both must be true).
  • || ➝ Logical OR (at least one true).
  • ! ➝ Logical NOT (reverses condition).

Logical Operators Example

let x = true, y = false;
console.log(x && y); // false
console.log(x || y); // true
console.log(!x);     // false

📌 Assignment Operators

Assignment operators are used to assign values to variables.

OperatorMeaningExample
=Assignx = 10
+=Add and assignx += 5
-=Subtract and assignx -= 3
*=Multiply and assignx *= 2

🔢 Bitwise Operators

Bitwise operators work on binary numbers at the bit level.

  • & ➝ AND
  • | ➝ OR
  • ^ ➝ XOR
  • ~ ➝ NOT
  • << ➝ Left Shift
  • >> ➝ Right Shift

❓ Other Useful Operators

  • typeof ➝ Returns type of a variable.
  • instanceof ➝ Checks if an object belongs to a class.
  • ?: ➝ Ternary operator (short if-else).
  • delete ➝ Deletes object property.
  • in ➝ Checks if property exists in an object.

Other Operators Example

let age = 20;
console.log(typeof age); // "number"
console.log(age >= 18 ? "Adult" : "Minor"); // "Adult"

Note

💡 Tip: Use === (strict equality) instead of == to avoid unexpected type coercion in JavaScript.
>>"Operators are the language of logic in programming – master them, and you master the flow of code." 🧠

🔗 Further Learning