3 ms·
No. The scenario you posit would be true if JavaScript were call-by-reference. JavaScript, however, is call-by-value. Primitive types (string, number, boolean)
by eonwe 13y ago
No. The scenario you posit would be true if JavaScript were call-by-reference.
JavaScript, however, is call-by-value.
Primitive types (string, number, boolean) are passed to functions by copying their value, and reference types (such as any object you create) are passed by copying the value of their reference.
The reference semantics can be observed in JavaScript in some cases, such as:
var i = 0;
for (i = 0; i < 10; i++) {
setTimeout(function() {
document.write("i is now " + i);
}, 10);
}
Here the inner function gets reference of the variable i instead of its value.