Chapter II · Variables & Values
Values
Values are the fundamental pieces of data that JavaScript programs store, evaluate, and manipulate.
01 What are Values?
At its core, a computer program is a machine designed to manipulate values. A value can be a number representing currency, a string of text in a chat message, a boolean switch, or a complex object.
Values exist independently of the variables that reference them. Variables simply act as pointers pointing to these values in memory.
02 Simple Example
Assigning different types of values to variables.
let userName = "Alice"; // String value
let userAge = 30; // Number value
let isLoggedIn = true; // Boolean value
The variables userName, userAge, and isLoggedIn point to distinct value types in memory.
03 How It Works
The JavaScript engine allocates memory for every value, tagging it with metadata describing its data type so operations can be performed correctly.
Values reside in memory while variables maintain references pointing directly to them.
04 Step-by-Step
An expression evaluates to a specific value.
The engine allocates memory to store that value.
A variable or data structure stores a reference pointing to that memory location.
05 Mental Model
"Values are objects in a warehouse, and variables are shipping tags attached to them."
Multiple tags can point to the same item, or items can exist temporarily without any tags attached.
06 Common Mistakes
Using typeof to check for arrays. typeof [] returns "object", not "array" — arrays are a specialized kind of object. Use Array.isArray(value) to check specifically for arrays.
Best practice: when you need to know whether copying a value is "safe" (independent) or "shared" (linked), ask first whether it's a primitive or an object — that single question predicts the behavior every time.
07 Interview Questions
Q: What are the two broad categories of values in JavaScript?
Primitives (string, number, boolean, null, undefined, symbol, bigint) and objects (which includes arrays and functions).
Q: Why does the distinction between primitives and objects matter in practice?
It determines whether a value is copied (primitives, by value) or shared by reference (objects) when assigned or passed to a function.
Key Takeaway
Values are the raw data entities in JavaScript, categorized into primitives and objects, referenced by variables.