Andrea Koutifaris
1 min readOct 27, 2018

--

I don’t think string literals are passed by value. Can you prove it?

String literals are immutable, so there is no need to copy their value.

Actually I think that everything is passed using reference-by-value. This means that J.S. passes values, but those values are always (or almost always) references to an Object.

(2).toString();
// prints "2", so 2 in this case is an object.

There is also the fact that J.S. may auto-wrap primitive types to the corresponding Object when needed. So it may be possible that primitive types like numbers are actually passed by value, unless you force the number to auto-wrap in an object.

By the way primitive types in JavaScript are immutable and possibly Objects.

Since in J.S. you cannot directly access to memory nor having a console that tells you directly or indirectly what the memory address is, I think you cannot prove either primitive values are passed by value or by reference.

But passing immutable objects by value is a waste of memory.

--

--

Andrea Koutifaris
Andrea Koutifaris

No responses yet