Variable
- In Turkish
- Değişken
In short
A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
What is a variable?
A variable gives a name to a piece of data so your code can refer to it later. Instead of repeating the value 0.2 everywhere, you can store it in a variable called taxRate and use that name. The name makes code easier to read, and changing the value in one place updates it everywhere it is used.
A useful analogy for a variable is a labeled box. The label is the variable's name, and the thing inside the box is its value. Behind the scenes, the program reserves a spot in memory for the value and uses the name to find it again.
Most languages have rules about what a variable can hold and where it can be used. Its type, such as number, string, or boolean, determines which operations make sense, and its scope determines which parts of the code can see it. In JavaScript, let declares a variable that can be reassigned, while const declares one that cannot.
Variables are often confused with constants. A constant is a name bound to a value that should not change after it is set, while a regular variable is expected to change. In JavaScript, const prevents reassignment but does not freeze an object's contents, so the properties of a const object can still be modified.
Key takeaways
- A variable is a name that points to a stored value.
- Its value can usually be read and changed while the program runs.
- Type controls what kind of data it holds; scope controls where it is visible.
- In JavaScript, use
constby default andletwhen the value must change.
Example
// let: the value can change later
let score = 0;
// const: the name can't be reassigned
const playerName = "Ada";
score = score + 10; // update the value
console.log(playerName, score); // "Ada" 10
// playerName = "Bob"; // TypeError: Assignment to constant variableReaders ask
What is the difference between let, const, and var in JavaScript?
let and const are block-scoped, meaning they only exist inside the nearest pair of curly braces, and const cannot be reassigned. var is the older, function-scoped keyword and is generally avoided in modern code because its scoping rules can cause surprising bugs.
What is variable scope?
Scope is the region of code where a variable can be accessed. A variable declared inside a function is local to that function, while one declared at the top level of a program may be visible everywhere, which is called global scope.
What is the difference between a variable and a constant?
A variable's value is expected to change while the program runs, while a constant is set once and should never change. Many style guides write true constants in uppercase, such as MAX_USERS, to make them easy to spot.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- AlgorithmProgramming Fundamentals, p. 2An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
- ScopeProgramming Fundamentals, p. 50Scope is the region of a program where a name, such as a variable or function, is visible and can be used, which decides what each line of code can reach.
Spotted a mistake or something missing on this page?Suggest an edit