Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

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

The primitive types (number, string, etc.) are passed by value. Still, objects are unknown because they can be both passed by value (in which case we consider that a variable holding an object is a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself).

Although it doesn't matter in the end, I want to know what is the correct way to present the arguments passing conventions. Is there an excerpt from the JavaScript specification, which defines what should be the semantics regarding this?

Answer*

Draft saved
Draft discarded
Cancel
5
  • @Amy Because that's describing pass by value, not pass by reference. This answer is a good one that shows the difference: stackoverflow.com/a/3638034/3307720 Commented Mar 27, 2019 at 17:03
  • @nasch I understand the difference. #1 and #2 are describing pass-by-ref semantics. #3 is describing pass-by-value semantics. Commented Mar 27, 2019 at 17:32
  • @Amy 1, 2, and 3 are all consistent with pass by value. To have pass by reference you would also need 4: assigning the reference to a new value inside the function (with the = operator) also reassigns the reference outside the function. This is not the case with Javascript, making it exclusively pass by value. When passing an object, you pass a pointer to the object, and you pass that pointer by value. Commented Mar 27, 2019 at 17:35
  • That isn't generally what is meant by "pass-by-reference". You've satisfied my query, and I disagree with you. Thanks. Commented Mar 27, 2019 at 17:38
  • "In my book that's called passing by reference." – In every single compiler book, interpreter book, programming language theory book, and computer science book ever written, it is not. Commented Jun 1, 2020 at 10:53

lang-js

AltStyle によって変換されたページ (->オリジナル) /