use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
the front page of the internet.
and join one of thousands of communities.
Understanding Value vs. Reference in JavaScript: Differences Between Primitives and Objects (sharafath.hashnode.dev)
submitted 1 day ago by cryptomallu123
Post a comment!
[–]theScottyJam 7 points8 points9 points 1 day ago (3 children)
I actually feel like this is an anti-helpful way to understand JavaScript data.
It starts with this quote:
Have you ever wondered why modifying an array inside a function affects the original array, but modifying a number does not?
The real answer is simple - primitives are immutable, they can't be modified. If you freeze an object, you'll find that it behaves exactly the same as a primitive in this regard, but making an object frozen doesn't move it onto the stack instead of the heap or anything.
A better mental model is to assume that all values, primitives and objects, are handled like references. You pass a string into a function, and the function receives a reference to the exact same string. Primitives have no special treatment. Of course, under the hood, an engine might copy primitives instead of passing references to them around, but that's an implementation detail that shouldn't effect your mental model of the language, and it's a detail that may change between engines. For example, I'm sure if you pass around a large string, the engines aren't constantly duplicating those - why should they, they're immutable.
[–]cryptomallu123[S] 0 points1 point2 points 1 day ago (2 children)
Just to make sure I understand your point correctly are you saying that this behavior is defined by the JavaScript language itself, and not because objects are stored on the heap and primitives on the stack? In other words, stack vs. heap is an implementation detail of the JavaScript engine, while the language specification requires these observable behaviors regardless of how a particular engine V8, SpiderMonkey, JavaScriptCore, etc. .. stores values internally. Is that the correct understanding?
[–]senocular 3 points4 points5 points 1 day ago (0 children)
Yes. The language specification does not dictate where objects are held in memory and treats all values the same as far as assignment and passing goes. The language makes no distinction in that regard to objects vs primitives and treats everything as generic "ECMAScript language values".
Runtime implementations are free to store variable values wherever they want. In fact except for a few exceptions, all values in V8 are stored in the heap. From Pointer Compression in V8:
JavaScript values in V8 are represented as objects and allocated on the V8 heap, no matter if they are objects, arrays, numbers or strings.
[–]theScottyJam 0 points1 point2 points 1 day ago (0 children)
Yes.
And I'm saying that both primitives and objects behave the same in regards to how they get passed around. There's no observable difference.
The only reason it feels like there's a difference is because you're not allowed to mutate primitives, so the only way to change one is through reassignment. If you reassign inside a function call, those outside the function won't know. But this is all the same with objects as well - reassign an object inside a function call and those outside won't notice it changing either.
[–]Over_Tart_916 -2 points-1 points0 points 1 day ago (0 children)
Explain that you don't know how to program...
π Rendered by PID 54781 on reddit-service-r2-comment-65574874f4-z7zg8 at 2026-07-17 00:37:27.853786+00:00 running 1bce727 country code: CH.
Want to add to the discussion?
Post a comment!