In JavaScript, Variables Don’t Have Types — Values Do
While studying JavaScript deeply, I came across an idea explained by Kyle Simpson in You Don’t Know JS:
Variables don’t have types. Values have types.
At first, this statement feels counter-intuitive, especially if you come from languages like Java, C++, or TypeScript. But understanding this concept is crucial for truly grasping how JavaScript works.
Let’s break it down.
The Traditional Mental Model (From Statically Typed Languages)
In many programming languages, variables themselves have types.
Example in Java:
int x = 10;
Here, the variable x is permanently an int. The type is attached to the variable, which means the variable can only store integer values.
If you try to assign something else:
x = "hello";
You’ll get a compile-time error because the variable’s type restricts what it can hold.
So the model looks like this:
variable (type) → value
JavaScript Works Differently
In JavaScript, variables are simply containers or references. They do not have an intrinsic type.
Instead, the value stored inside the variable has the type.
Example:
let x = 10;
Here:
10is a number valuexis simply a reference to that value
If we check the type:
typeof x // "number"
The typeof operator returns the type of the value currently stored, not the type of the variable.
Variables Can Hold Different Types
Since variables themselves aren’t typed, they can store completely different kinds of values over time.
let x = 10; // number
x = "hello"; // string
x = true; // boolean
What’s happening internally is closer to this mental model:
x → 10
x → "hello"
x → true
The variable x stays the same, but the value it points to changes.
Thinking of Variables as Labels
A helpful way to visualize JavaScript variables is to think of them as labels attached to values.
Example:
let name = "Rahul";
Conceptually:
name ───► "Rahul"
(string)
If we later write:
name = 42;
The label now points to a different value:
name ───► 42
(number)
The label didn’t change — only the value it references.
Why This Matters in JavaScript
Understanding that values carry types explains many behaviors in JavaScript.
Example:
let x = 5;
x + 2 // 7
x = "5";
x + 2 // "52"
When x is a number, addition occurs.
When x is a string, JavaScript performs string concatenation.
The behavior changes because the type of the value changed.
JavaScript Is Dynamically Typed
This design makes JavaScript a dynamically typed language.
Types still exist, but they are determined at runtime, and they belong to values rather than variables.
This flexibility is powerful but also requires developers to understand how JavaScript handles values and type conversions.
Final Thoughts
The key takeaway is simple but important:
Variables hold values.
Values have types.
Once you internalize this idea, many confusing aspects of JavaScript — such as type coercion, dynamic behavior, and runtime operations — start to make much more sense.
And it becomes much easier to reason about how JavaScript code actually works under the hood.
