Is JavaScript language a pass-by-reference or pass-by-value language?
Also is it different for primitive types Vs for objects ?
It uses an evaluation strategy named call by sharing actually.
All types are passed by value. There's no pass-by-reference, otherwise you'd be able to modify contents of variables declared at the call site of a function. Usually people say that objects are passed by reference in JS. They're actually passed by sharing, which means you can modify an object's properties, and these changes will be visible to those that hold a reference to that object, but the reference in itself is not modifiable.
Objects are passed by reference while primitives are passed by value.
Note, that primitive values include the following:
undefined
null
You can find some more details at MDN on Functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With