Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JavaScript pass-by-reference or pass-by-value? [duplicate]

Is JavaScript language a pass-by-reference or pass-by-value language?

Also is it different for primitive types Vs for objects ?

like image 635
copenndthagen Avatar asked Feb 23 '13 18:02

copenndthagen


2 Answers

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.

like image 91
Ionuț G. Stan Avatar answered Sep 28 '22 08:09

Ionuț G. Stan


Objects are passed by reference while primitives are passed by value.

Note, that primitive values include the following:

  • number
  • String
  • boolean
  • undefined
  • null

You can find some more details at MDN on Functions.

like image 20
Sirko Avatar answered Sep 28 '22 07:09

Sirko