var a = 10;
console.log(a); // Output: 10

var b;
console.log(b); // Output: undefined
let a = 10;
console.log(a); // Output: 10

let b;
console.log(b); // Output: undefined
const age = 30;
const age; // Uncaught SyntaxError: Missing initializer in const declaration
if () { // if block
    // ...
} else { // else block
    // ...
}

function exampleFn() { // function block
    // ...
}

{ // standalone block (rarely used)
  let temp = 10; // temp is only valid within this block
}

for () {  // for block
    // ...
}
/* Global scope */
var globalVar = "Global variable"; // Accessible throughout the code

function exampleFunction() {
    /* Function scope */
    var functionVar = "Function variable"; // Accessible throughout the function

    if (true) {
        var blockVar = "Block variable"; // Declared in if block, but accessible throughout the function
        console.log(functionVar); // Output: "Function variable"
        console.log(blockVar);    // Output: "Block variable"
    }

    // Check if blockVar is accessible outside if block
    console.log(blockVar); // Output: "Block variable"
}

exampleFunction();
console.log(globalVar); // Output: "Global variable"
console.log(functionVar); // ReferenceError: functionVar is not defined
/* Global scope */
let globalVar = "Global variable"; // Accessible in the top-level block of this file or script

function exampleFunction() {
    /* Function block scope */
    let functionVar = "Function block variable"; // Accessible within the function block

    if (true) {
        let innerBlockVar = "If block variable"; 
        console.log(functionVar);   // Output: "Function block variable"
        console.log(innerBlockVar); // Output: "If block variable"
    }

    // Cannot access innerBlockVar outside if block
    // console.log(innerBlockVar); // ReferenceError: innerBlockVar is not defined
}

if (true) {
    let ifBlockVar = "If block variable";
    console.log(ifBlockVar); // Output: "If block variable"
}

exampleFunction();
console.log(globalVar); // Output: "Global variable"
// console.log(functionVar); // ReferenceError: functionVar is not defined
// console.log(ifBlockVar); // ReferenceError: ifBlockVar is not defined
/* Global scope */
const globalVar = "Global variable"; // Accessible in the top-level block of this file or script

function exampleFunction() {
    /* Function block scope */
    const functionVar = "Function block variable"; // Accessible within the function block

    if (true) {
        const innerBlockVar = "If block variable"; 
        console.log(functionVar);   // Output: "Function block variable"
        console.log(innerBlockVar); // Output: "If block variable"
    }

    // Cannot access innerBlockVar outside if block
    // console.log(innerBlockVar); // ReferenceError: innerBlockVar is not defined
}

if (true) {
    const ifBlockVar = "If block variable";
    console.log(ifBlockVar); // Output: "If block variable"
}

exampleFunction();
console.log(globalVar); // Output: "Global variable"
// console.log(functionVar); // ReferenceError: functionVar is not defined
// console.log(ifBlockVar); // ReferenceError: ifBlockVar is not defined
/* Initial declaration and assignment */
var myValue = 10;
console.log(myValue); // Output: 10

/* Reassignment */
myValue = 20;
console.log(myValue); // Output: 20

/* Redeclaration */
var myValue = "Hello";
console.log(myValue); // Output: "Hello" (redeclared without error)
// Redeclaring with let in the same scope throws an error
let exampleLet = 10;
let exampleLet = 100; // Uncaught SyntaxError: Identifier 'exampleLet' has already been declared
 // However, redeclaration is allowed in a different scope:
let exampleLet = 10;
console.log(exampleLet); // Output: 10

if (true) {
    let exampleLet = 100;
    console.log(exampleLet); // Output: 100
}

console.log(exampleLet); // Output: 10
// Global scope
let globalVar = "Global variable";
console.log(globalVar); // "Global variable"

// Reassignment
globalVar = "Reassigned global variable";
console.log(globalVar); // "Reassigned global variable"

if (true) {
    // Block scope
    let blockVar = "Block variable";
    console.log(blockVar); // "Block variable"

    // Reassignment
    blockVar = "Reassigned block variable";
    console.log(blockVar); // "Reassigned block variable"
}

// Outside the block, not accessible
// console.log(blockVar); // ReferenceError
// Redeclaring a const variable in the same scope throws an error
const exampleConst = 10;
const exampleConst = 100; // Uncaught SyntaxError: Identifier 'exampleConst' has already been declared
// Redeclaration is allowed in a different scope:
const exampleConst = 10;
console.log(exampleConst); // Output: 10

if (true) {
    const exampleConst = 100;
    console.log(exampleConst); // Output: 100
}

console.log(exampleConst); // Output: 10
const exampleConst = 10;

/* Cannot change the variable's value using the assignment operator (=) with the same identifier */
exampleConst = 100; // Uncaught TypeError: Assignment to constant variable
// Declare a plain object
const person = {
    name: 'Alice',
    age: 30
};

// 1. Add a property
person.city = 'Seoul'; // Add a new property
console.log(person); // Output: { name: 'Alice', age: 30, city: 'Seoul' }

// 2. Update a property
person.age = 31; // Update an existing property
console.log(person); // Output: { name: 'Alice', age: 31, city: 'Seoul' }

// 3. Remove a property
delete person.city; // Remove a property
console.log(person); // Output: { name: 'Alice', age: 31 }
// Declare an array
const numbers = [1, 2, 3];

// 1. Add an element
numbers.push(4); // Add an element at the end of the array
console.log(numbers); // Output: [1, 2, 3, 4]

// 2. Update an element
numbers[0] = 10; // Update the first element
console.log(numbers); // Output: [10, 2, 3, 4]

// 3. Remove an element
numbers.pop(); // Remove the last element
console.log(numbers); // Output: [10, 2, 3]
/*
 * The variable myName declared below with var
 * is hoisted as if it is positioned at the top of the scope.
 * Only the declaration is hoisted; the assigned value remains in place.
 *
 * Since there is no value assigned at declaration,
 * referencing the variable before its actual declaration
 * initializes it as undefined.
*/

// Reference before the actual declaration
console.log(myName); // Output: undefined (only the declaration is hoisted, value assigned as undefined)

/* Position in code where the var variable is declared */
var myName = "John";
// Start of TDZ
// ↓
console.log(myVar); // ReferenceError: Cannot access 'myVar' before initialization
// ↑
// End of TDZ

let myVar = 10;
// Start of TDZ
// ↓
console.log(myVar); // ReferenceError: Cannot access 'myVar' before initialization
// ↑
// End of TDZ

const myVar = 10;