Scope:
// var example
function varScope() {
if (true) {
var x = 10;
}
console.log(x); // Output: 10 (var is function-scoped)
}
// let example
function letScope() {
if (true) {
let y = 10;
}
console.log(y); // Output: ReferenceError: y is not defined (let is block-scoped)
}
varScope();
letScope();
Hoisting:
// var example
console.log(a); // Output: undefined (var is hoisted but not initialized)
var a = 5;
// let example
console.log(b); // Output: ReferenceError: b is not defined (let is hoisted but not initialized)
let b = 5;
Re-declaration:
// var example
var c = 10;
var c = 20; // No error, re-declaration allowed
console.log(c); // Output: 20
// let example
let d = 10;
let d = 20; // Error: Identifier 'd' has already been declared
Global Object Property:
// var example
var e = 'globalVar';
console.log(window.e); // Output: 'globalVar' (var becomes a global object property)
// let example
let f = 'globalLet';
console.log(window.f); // Output: undefined (let does not become a global object property)