Variable
Variable
A name attached to a memory location that stores data in a program. Understanding the differences between let, const, and var and scope lets you write stable, predictable code.
Variable
A name attached to a memory location that stores data in a program. Understanding the differences between let, const, and var and scope lets you write stable, predictable code.
A variable is a name attached to a memory location used to store and reference data in a program. It means "a value that can change," so its value can change while the program runs. For example, if you declare let count = 0;, the value 0 is stored somewhere in memory, and we can access and change that value through the name "count". Variables are the most fundamental concept in programming and are essential in every language.
Scope refers to the range in which a variable can be accessed. A variable in global scope can be accessed from anywhere in the program, while a variable in local scope can only be accessed inside the function where it is declared. Block scope, introduced in ES6, is valid only inside { } curly braces. JavaScript's let and const follow block scope, but var follows function scope, which can cause unintended bugs.
The differences between let, const, and var in JavaScript are very important. var is the pre-ES6 way of declaring variables; it has function scope and is initialized to undefined when hoisted. let has block scope and is hoisted, but accessing it before initialization causes a TDZ (Temporal Dead Zone) error. const follows the same scope rules as let, but it must be initialized at declaration and cannot be reassigned. However, the inner values of an object or array can be changed.
A naming convention directly affects the readability and maintainability of code. In JavaScript, camelCase is used, and in Python, snake_case is used. A variable name should clearly indicate its role. elapsedTimeInDays is better than d, and currentTemperature is better than temp. Avoid abbreviations, and starting boolean variables with is/has/can, as in isActive and hasPermission, makes their meaning clear.
// 1. var - ν¨μ μ€μ½ν, νΈμ΄μ€ν μ undefinedλ‘ μ΄κΈ°ν console.log(varVariable); // undefined (μλ¬ μλ!) var varVariable = "varλ‘ μ μΈ"; // 2. let - λΈλ‘ μ€μ½ν, TDZ μ‘΄μ¬ // console.log(letVariable); // ReferenceError: TDZ let letVariable = "letμΌλ‘ μ μΈ"; // 3. const - μ¬ν λΉ λΆκ°, κ°μ²΄ λ΄λΆλ λ³κ²½ κ°λ₯ const PI = 3.14159; // PI = 3.14; // TypeError: μ¬ν λΉ λΆκ° const user = { name: "κΉκ°λ°", age: 25 }; user.age = 26; // κ°μ²΄ λ΄λΆ λ³κ²½μ κ°λ₯! console.log(user); // { name: "κΉκ°λ°", age: 26 } // 4. μ€μ½ν λΉκ΅ function scopeExample() { if (true) { var functionScoped = "var: ν¨μ μ€μ½ν"; let blockScoped = "let: λΈλ‘ μ€μ½ν"; } console.log(functionScoped); // "var: ν¨μ μ€μ½ν" // console.log(blockScoped); // ReferenceError } // 5. for 루νμμμ μ°¨μ΄ (ν΄λ‘μ λ¬Έμ ) for (var i = 0; i < 3; i++) { setTimeout(() => console.log("var:", i), 100); } // μΆλ ₯: 3, 3, 3 (μλμΉ μμ κ²°κ³Ό!) for (let j = 0; j < 3; j++) { setTimeout(() => console.log("let:", j), 100); } // μΆλ ₯: 0, 1, 2 (μλν κ²°κ³Ό)
// 1. μ μ λ³μ (κ°λ₯νλ©΄ νΌνκΈ°) let globalCount = 0; // μ΄λμλ μ κ·Ό κ°λ₯ - μν! // 2. μ§μ λ³μμ μ€μ½ν μ²΄μΈ const outerValue = "μΈλΆ"; function outer() { const middleValue = "μ€κ°"; function inner() { const innerValue = "λ΄λΆ"; // μ€μ½ν 체μΈ: inner -> outer -> global console.log(innerValue); // "λ΄λΆ" (μμ μ μ€μ½ν) console.log(middleValue); // "μ€κ°" (μΈλΆ μ€μ½ν) console.log(outerValue); // "μΈλΆ" (μ μ μ€μ½ν) } inner(); } // 3. ν΄λ‘μ λ₯Ό νμ©ν private λ³μ function createCounter() { let count = 0; // private λ³μ (μΈλΆμμ μ§μ μ κ·Ό λΆκ°) return { increment() { count += 1; return count; }, decrement() { count -= 1; return count; }, getCount() { return count; } }; } const counter = createCounter(); console.log(counter.increment()); // 1 console.log(counter.increment()); // 2 console.log(counter.getCount()); // 2 // counter.count; // undefined (μ§μ μ κ·Ό λΆκ°)
let d = new Date() - startTime;, but it's hard to tell right away what d means. Could you change the variable name to something clearer?const elapsedTimeMs = Date.now() - startTime; work? I changed it to const because this value isn't modified later.let flag = true; below would be better with a meaningful name like isProcessing or shouldContinue.undefined when hoisted, so accessing it before its declaration gives undefined without an error. let and const, on the other hand, are hoisted but not initialized, so accessing them before their declaration causes a TDZ (Temporal Dead Zone) error. Because of this difference, let/const are safer, and these days we use let/const instead of var.Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
no-var rule.temp, data, and result, use specific names like userProfile and filteredProducts.