SmartCodingTips

JavaScript Operators

Operators in JavaScript allow you to perform calculations, compare values, assign data, and more. They're categorized into several types based on what they do.

➕ 1. Arithmetic Operators

let x = 10;
let y = 3;

x + y  // 13 (Addition)
x - y  // 7  (Subtraction)
x * y  // 30 (Multiplication)
x / y  // 3.33... (Division)
x % y  // 1  (Modulus)
x ** y // 1000 (Exponentiation)

🟰 2. Assignment Operators

let a = 5;

a += 2;  // a = a + 2 => 7
a -= 1;  // a = a - 1 => 6
a *= 3;  // a = a * 3 => 18
a /= 2;  // a = a / 2 => 9
a %= 4;  // a = a % 4 => 1

🔍 3. Comparison Operators

5 == '5'   // true (loose equality)
5 === '5'  // false (strict equality)
5 != '5'   // false
5 !== '5'  // true
5 > 3      // true
5 < 10     // true
5 >= 5     // true
3 <= 2     // false
⚠️ Tip: Use === and !== for strict comparisons to avoid type coercion.

🤔 4. Logical Operators

true && true   // true
true && false  // false
true || false  // true
!true          // false

🔄 5. Increment & Decrement

let count = 1;

count++; // Post-increment
++count; // Pre-increment
count--; // Post-decrement
--count; // Pre-decrement

🧠 6. Ternary (Conditional) Operator

let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";

Use this for inline if-else logic.

📚 7. Type Operators

  • typeof – Returns the type of a variable
  • instanceof – Checks if an object is an instance of a class
typeof "Hello"       // "string"
person instanceof Object  // true
Practice: Try combining arithmetic, logical, and ternary operators in short functions to see how they behave.