Variable
Variable
The name of a memory location that stores data. Depending on the type, it holds numbers, strings, objects, and so on.
Variable
The name of a memory location that stores data. Depending on the type, it holds numbers, strings, objects, and so on.
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.
// 변수 선언 방식
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 = {}; // 에러! (재할당 불가)
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free