Skip to main content

Variable

In Turkish
Değişken
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/variable

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 const by default and let when the value must change.

Example

Declaring variables in JavaScriptjavascript
// 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 variable

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings