Learning on Web Dev Open is free for all.

Systems Thinking > What a variable actually holdsTwo kinds of thing
Phase 03What a variable actually holds133 of 434

Two kinds of thing

Everything in JavaScript is either a value you hold or an address you follow, and almost nothing tells you which.

Concept12 minAI pair

A binding holds a number, a string, a boolean, a bigint, a symbol, null or undefined directly. For anything else, objects, arrays, functions, dates, maps, class instances, it holds a reference: a small opaque handle pointing at something on the heap. Two bindings can hold the same handle, and then there is one object with two names.

This is the whole of it, and it explains the behaviour people describe as "JavaScript passes by reference", which is not quite true. Arguments are always passed by value; it is just that the value can be a reference. Reassigning a parameter inside a function never touches the caller. Mutating what the parameter points at always does. Those are different operations that look almost identical on the page.

The practical consequence is that a function signature tells you nothing about whether the function will change your data. Nothing in the language marks it. That absence is why the rest of this lesson exists, and why the convention of not mutating your inputs is worth adopting before you have been bitten.

You should now be able to

  • Say what a variable binding actually stores
  • Predict whether a function can change its caller’s data
Ask the community

Loading…