Lab 02: Variables, Data Types & Type Coercion

🎯 Objective

Master JavaScript's type system — var, let, const, primitive types, type coercion, and the quirks that trip up every JavaScript developer.

📚 Background

JavaScript has 7 primitive types: number, string, boolean, null, undefined, symbol, and bigint — plus the object type for everything else. JavaScript performs implicit type coercion — automatically converting types in operations — which causes famous bugs like "5" + 3 === "53". Understanding these behaviors separates confident JS developers from frustrated ones.

⏱️ Estimated Time

30 minutes

📋 Prerequisites

  • Lab 1: Hello World & JavaScript Basics

🛠️ Tools Used

  • Node.js 20

  • Docker (innozverse-js:latest)

🔬 Lab Instructions

Step 1: var vs let vs const

📸 Verified Output:

💡 Rule of thumb: Use const by default. Use let only when you need to reassign. Never use var in modern JavaScript.

Step 2: Primitive Types

📸 Verified Output:

Step 3: String Type

📸 Verified Output:

Step 4: Boolean, null, undefined

📸 Verified Output:

Step 5: Type Coercion — The Famous Quirks

📸 Verified Output:

💡 Always use === (strict equality) in JavaScript. == performs type coercion and leads to surprising bugs.

Step 6: typeof and Type Checking

📸 Verified Output:

💡 typeof null === "object" is a famous JavaScript bug kept for backward compatibility. Use v === null to check for null specifically. Use Array.isArray() to check for arrays.

Step 7: Number Conversions

📸 Verified Output:

Step 8: Destructuring Assignment

📸 Verified Output:

✅ Verification

Expected output:

🚨 Common Mistakes

  1. "5" + 3 === "53" — use explicit Number("5") + 3 when you want arithmetic.

  2. == null is OK, == undefined is OK — but === null is preferred for clarity.

  3. typeof null === "object" — always use === null not typeof for null checks.

  4. const doesn't deep-freeze objects — properties can still be mutated.

  5. NaN !== NaN — use Number.isNaN(x) not x === NaN.

📝 Summary

  • Use const by default, let when reassigning, never var

  • 7 primitives: number, string, boolean, null, undefined, symbol, bigint

  • Always use === (strict equality) — avoid == coercion surprises

  • typeof null === "object" is a JS bug — use === null for null checks

  • Array.isArray() to check arrays — typeof just returns "object"

  • Falsy: false, 0, "", null, undefined, NaN; everything else is truthy

🔗 Further Reading

Last updated