Skip to main content

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*

17
  • 9
    I think you accidentally flipped your definitions of passed-by-value and passed-by-reference... "passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself)" Commented Aug 24, 2014 at 19:25
  • 12
    Yes. Regardless of syntax, in any function call in any programming language, pass-by-reference means the data associated with the passed variable is not copied when passed to the function, and thus any modifications made by the function to the passed variable will be retained in the program after the function call terminates. Pass-by-value means the data associated with the variable is actually copied when passed to the function and any modifications made by such function to such variable will be lost when the variable goes out of scope of the function's body when the function returns. Commented Oct 29, 2014 at 11:38
  • 10
    This old question is somewhat toxic because its heavily-upvoted answer is incorrect. JavaScript is strictly pass-by-value. Commented Nov 28, 2015 at 0:00
  • 10
    @DanailNachev The terminology is regrettably confusing. The thing is, "pass by value" and "pass by reference" are terms that predate a lot of more modern programming language features. The words "value" and "reference" refer specifically to the parameter as it appears in the function call expression. JavaScript always evaluates each expression in a function call parameter list before calling the function, so the parameters are always values. The confusing part is that references to objects are common JavaScript values. That doesn't make it a "pass by reference" language, however. Commented Dec 15, 2015 at 22:57
  • 6
    @DanailNachev "pass by reference" specifically means that if you have var x=3, y=x; f(x); alert(y === x); then function f() can make the alert report false and not true. In JavaScript, that's not possible, so it's not pass-by-reference. It's good that it's possible to pass references to modifiable objects, but that's not what "pass by reference" means. As I said, it's a shame that the terminology is so confusing. Commented Dec 15, 2015 at 23:00