3 ms·
JavaScript is pass-by-reference for all Objects and Arrays. Value types are pass-by-value. let obj = {a: 1} function foo(arg) { return obj =
by potch 9y ago
JavaScript is pass-by-reference for all Objects and Arrays. Value types are pass-by-value.
let obj = {a: 1}
function foo(arg) {
return obj === arg;
}
foo(obj) // true
The === operator operates on object not by comparing their value, but by comparing their memory reference [1]. A function argument variable can be reassigned using = in the function body, but that changes which location in memory the reference points to and isn't somehow "proof" of pass-by-value.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Equality_operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- shkkmo 9y agoJavascript is pass-reference-by-value for Objects and Arrays, see: https://jsfiddle.net/t73ykuj0/ https://jsfiddle.net/t73ykuj0/ If javascript was pass-by-reference for Objects, your code could read: arg = {a: 2}; return obj === arg; and still behave the same.