5

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

Also is it different for primitive types Vs for objects ?

0

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Which is akin to saying that you're given a pointer to an object, and you can then modify that object's properties, from the current scope, but if you try to reassign the value of the object, internally, you're merely resetting your pointer.
4

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.

1 Comment

Everything in JavaScript is passed by Value. It's just that when you pass an Object, you are passing the reference to the Object, not the Object itself, so a copy of the reference is passed. Even the MDN link you shared states this.
0

Everything but primitives are passed by reference.
Just about everything in JavaScript is an object. As Sirko said, Objects are passed by reference.

So functions/arrays/objects are all passed by reference, whether you're talking about the root object attached to a var, or you're talking about an object's property/method, chained 3 dots deep, or you're talking about an object in an array, as a property of an object, in an array of objects...

35 Comments

Everything in JavaScript is passed by Value. It's just that when you pass an Object, you are passing a COPY (which is pass by value) of the reference to the Object, not the Object itself, so a copy of the reference is passed.
@ScottMarcus That's ... really not helpful to someone who is learning JS. You're getting into particulars of how a C++ / Rust VM runtime is going to manage access to memory. Generally, people asking this question want to know "If I pass in an array, and then I change an element of the array, outside of the function has the original array changed" and the answer is, of course, yes. Whether a smart-pointer (or equivalent) is copied or passed in directly doesn't change the language behaviour, in terms of new-user expectations.
On the contrary. As someone who has been a professional IT trainer for over 30 years and worked with JavaScript since Netscape invented it, I can tell you with certainty that my comment is exactly what new learners need to hear as there is much confusion over this, not only in JavaScript, but also in .NET and other languages. Your (and Sirko's) answer is wholly incorrect. JavaScript has no pass-by-reference and that was exactly what the OP was asking about. My comment in no way gets into particulars of how C++/Rust handles memory. My comment is clear and to the point.
You are most likely having the same confusion that many do, which is that there are "value types" (primitives) and "reference types" (objects), but both types are passed by value, not by reference.
I’m not confused. At all. I am aware that there is no differentiation, technically. People asking these questions tend to be early in their journey and have picked up terms from people who teach everything as if everything was built up from C, or viewed through the lens of C. JS has no concept of pointers, so passing a copy of a pointer has no meaning to someone who is coming to JS from, say, Scratch, or Haskell (aside from C-interop modules). I’m not about to suggest they learn Rust’s borrow-checker, or C++ Move semantics, even a decade later, pedantically accurate or not.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.