Skip to content
Devgo

Variable

A named container for a value in code. Variables let you store, reference, and change data. In dynamically typed languages (JS, Python) a variable can hold any type; in statically typed languages (Rust, Java) the type is fixed at declaration.

When to use
  • Always — every program uses variables.
When not to
  • Never.

Definition

A variable associates a name with a storage location. You assign a value (x = 5), read it (print x), and reassign it (x = 10). determines where a variable is visible. Mutable variables can change; constants/immutable variables cannot.

Why it exists

To give names to data so code is readable and reusable, instead of repeating literal values everywhere.

How it works

The language allocates memory for the value and binds the name to it. Reading the name returns the value; assigning replaces it. rules (block, , module) control visibility. Garbage collection or manual management reclaims memory when variables go out of scope.

Scaling limits

Not applicable — variables are a fundamental concept. The challenge is naming and scoping well in large codebases.

Cost shape

Free.

Alternatives

  • Constants — Immutable, safer for values that shouldn't change.

Failure modes

  • Poor naming making code unreadable.
  • leaks causing bugs.
  • Mutable global state causing race conditions.

, ,