JavaScript Variables and Data Types
| Keyword | Scope | Re-assign? | Re-declare? | Hoisted? |
|---|---|---|---|---|
| var | Function | Yes | Yes | Yes (as undefined) |
| let | Block | Yes | No | Yes, but in the temporal dead zone |
| const | Block | No | No | Yes, but in the temporal dead zone |
Modern ruleUse
const by default, let when the value must change, and avoid var.Data Types
let n = 42; // number
let big = 9007199254740993n; // bigint
let s = "hello"; // string
let ok = true; // boolean
let nothing = null; // null
let notSet; // undefined
let id = Symbol("id"); // symbol
let obj = { a: 1 }; // object (arrays and functions are objects too)
console.log(typeof n, typeof s, typeof nothing, typeof notSet, typeof obj);
number string object undefined object
Famous quirk
typeof null returns "object". It's a bug from the first version of JavaScript that was kept for compatibility.Template Literals
const name = "Asha", marks = 88;
console.log(`${name} scored ${marks} (${marks >= 40 ? "Pass" : "Fail"})`);
Asha scored 88 (Pass)
Track your progress through the JavaScript course