💻 Programming

Variable

Variable

The name of a memory location that stores data. Depending on the type, it holds numbers, strings, objects, and so on.

📖 Detailed Explanation

A variable is a named memory space in a program for storing and referencing data. Through variables, you can store various values such as numbers, strings, and objects and use them later.

Variables have a scope (range). A global variable is accessible throughout the entire program, while a local variable is valid only within the block where it is declared. In JavaScript, var has function scope, and let/const have block scope.

Immutability of variables is an important concept. JavaScript's const, Python's conventional uppercase variable names, and Rust's immutable-by-default bindings indicate that a value does not change. Immutable variables reduce bugs and make code easier to understand.

The way variables are declared differs depending on the type system. Statically typed languages (Java, TypeScript) specify the type explicitly, while in dynamically typed languages (Python, JavaScript) the type is determined at runtime. In languages that support type inference, the compiler automatically infers the type.

💻 Code Example

JavaScript
// 변수 선언 방식
var oldWay = "함수 스코프 (권장하지 않음)";
let mutableVar = "블록 스코프, 재할당 가능";
const immutableRef = "블록 스코프, 재할당 불가";

// 스코프 예시
function scopeExample() {
    let blockScoped = "함수 내부";

    if (true) {
        let innerVar = "블록 내부";
        var functionVar = "함수 스코프";
        console.log(blockScoped);  // 접근 가능
    }

    console.log(functionVar);  // 접근 가능 (var)
    // console.log(innerVar);  // 에러! (let은 블록 스코프)
}

// 호이스팅
console.log(hoisted);  // undefined (var는 호이스팅)
var hoisted = "값";
// console.log(notHoisted);  // 에러! (let/const는 TDZ)
let notHoisted = "값";

// 구조 분해 할당
const user = { name: "김개발", age: 30 };
const { name, age } = user;

const numbers = [1, 2, 3];
const [first, second] = numbers;

// const와 객체 변경
const config = { debug: true };
config.debug = false;  // 가능 (참조는 불변, 내용은 변경 가능)
// config = {};  // 에러! (재할당 불가)

🗣️ Examples of Practical Conversations

Junior Developer
"Is it okay to use a global variable? It's a configuration value needed in several places."
Senior developer
"It's better to avoid global variables. If you manage it with the singleton pattern or dependency injection, testing is easy and side effects are reduced."
Junior Developer
"How should we manage environment variables?"
Interviewer
"Please explain the difference between var, let, and const in JavaScript."
Candidate
"var is function-scoped and hoisted. let is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned. In modern code, use const by default, and use let only when necessary."
Interviewer
"How does variable capture work in a closure?"
Candidate
"A closure remembers the scope at the time of its creation. With var, there is a problem where only the last value is captured in a loop, but let solves this problem by creating a new binding for each iteration."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer
"This variable name is too short. Instead of 'x', use a meaningful name like 'userCount'."
Author
"I used a short name because it's a loop index, but I'll change variables that carry meaning to clear names."
Reviewer
"Also, you declared it with let but there's no reassignment. If you change it to const, the intent becomes clearer."

⚠️ Cautions

🔗 Related Terms

📚 Learn More