JavaScript Variables and Data Types

KeywordScopeRe-assign?Re-declare?Hoisted?
varFunctionYesYesYes (as undefined)
letBlockYesNoYes, but in the temporal dead zone
constBlockNoNoYes, but in the temporal dead zone
Modern ruleUse const by default, let when the value must change, and avoid var.

Data Types

JavaScript
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 quirktypeof null returns "object". It's a bug from the first version of JavaScript that was kept for compatibility.

Template Literals

JavaScript
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