Example 1 — var is undefined
Declared but not assigned yet.
JavaScript
console.log(x);
var x = 2;How It Works
- Declaration hoisted; assignment stays in place.
Hoisting means declarations are processed before code runs. function declarations are fully available. var is declared early but starts as undefined. let/const are in the temporal dead zone until initialized.
console.log(typeof f);
function f() { return 1; }
console.log(f());Declared but not assigned yet.
console.log(x);
var x = 2;TDZ throws.
try { console.log(y); let y = 1; } catch (e) { console.log(e.name); }Readable style.
function g() { return "ok"; }
console.log(g());CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful