2 ms·
That's irrelevant. Look, same example but with an object. let a = { bar: 'baz' }; function foo( a ) { a = { beep: 'boop' }; } f
by kevincennis 9y ago
That's irrelevant. Look, same example but with an object.
let a = { bar: 'baz' };
function foo( a ) {
a = { beep: 'boop' };
}
foo( a );
console.log( a ); // { bar: 'baz' }
- n0us 9y agoAnother way to think about it is that you just get a new reference to the same object, inside foo you are changing what 'a' refers to instead of what 'b' refers to. Using 'let' to declare 'b' is not necessary and using 'const' works just as well. $ node > const b = { bar: 'baz' } > function foo(a){ a = {beep: 'boop'}; } > foo(b) > console.log(b) { bar: 'baz' } >
- lowkeyokay 9y agoI thought this was just a matter of scope. a declared outside of the function is not the same as the one declared as a parameter.