Understand let, const, var, and how JavaScript handles variable scope.
1// const — cannot be reassigned2const name = "Skill-Forge";3// name = "Other"; // TypeError!4 5// let — can be reassigned6let score = 0;7score = 100;8console.log(score); // 1009 10// var — avoid in modern JS11var old = "legacy";12console.log(old);100 legacy
1// Block scope with let2if (true) {3 let x = 10;4 console.log(x); // 105}6// console.log(x); // ReferenceError!7 8// Function scope with var9function example() {10 if (true) {11 var y = 20;12 }13 console.log(y); // 20 — var leaks out of the if block!14}15example();10 20
1const str = "hello"; // string2const num = 42; // number3const bool = true; // boolean4const empty = null; // object (known quirk)5const notDefined = undefined; // undefined6 7console.log(typeof str); // "string"8console.log(typeof num); // "number"9console.log(typeof bool); // "boolean"10console.log(typeof empty); // "object""string" "number" "boolean" "object"