I recently watched a course about javascript on Udemy. It was a great course from a great instructor who provided great content - except for one thing. He said that
"Javascript is pass by reference for objects and pass by value for primitives."
This statement is only 50% correct 🌓 The incorrect half - as it is hinted in the title of the post - is the first half. Javascript doesn't use pass by reference for objects. It is commonly stated that javascript uses pass by value for objects too and that the value for objects is actually the reference of that object. That's what I thought about javascript's evaluation strategy, too. But that statement is not 100% correct either 😲😓💥 TIL it uses call by sharing for objects 😲 💥
So let's dive into these aforementioned evaluation strategies and try to understand the differences between them and hopefully clear up the confusion:
Pass by reference vs. Pass by value vs. Call by sharing
Pass by reference
I want to use an analogy here. Let's consider soccer. In soccer there is one ball and players pass the ball to each other (to simply put it). If the ball so to say gets muddy, dirty and a player passes the ball to another player the second player will receive the muddy, dirty ball. And
let's assume the players decided to clean the ball because it got so dirty that it made it impossible play the game. When game continues with the clean ball none of the players will receive the old dirty, muddy ball they will all be playing with new clean and shiny ball.
This is pass by reference. The ball here is the reference. In pass by reference function receives a reference to the variable. If any change occurs in the reference every piece of code which uses that reference will be affected and will use the changed version of the reference.
Pass by value
Let's use another analogy here. Let's consider books. A person writes a book and publishes it. If you want to read that book you go to the bookstore and get yourself a copy of that book. You don't go to the author's house and grab the original writings. And if you decide to underline a sentence in your book the other books don't get the same sentence underlined, you only change your copy.
This is pass by value. The book here is the value. In pass by value function receives a copy of the value. If the function changes the value, the change will be limited in the function's scope.
Call by sharing
Unfortunately I don't have an analogy for this evaluation strategy 😥 So I'm just going to write it as it is.
In call by sharing the function receives a copy of the reference to the object. If the object gets mutated, every piece of code which uses that object will use the changed version of the object.
By now the evaluation strategies playing part in the confusion and the differences they have should be clear which means we can skip to the reason behind the confusion part 🎉🎊 Let's consider the following code snippet:
When john
is passed to confusionCreator()
, actually the reference to john
is copied and passed to confusionCreator()
. Since there is a copying process it resembles pass by value.
When name
attribute is updated inside confusionCreator()
but affected the code pieces using john
outside the confusionCreator()
it resembles pass by reference.
These resemblances are the reason behind the confusion around javascript's evaluation strategies.
I hope this post helped in some way to clear up the confusion around javascript's evaluation strategies.
Top comments (9)
You say here that:
Isn't that what call by sharing is? I found this SO answer that says the same thing
Yes, that is what call by sharing is 👍 When I google "is javascript pass by value" I see results which says "javascript is pass by value". Only a few of them mentions the term call by sharing. So I'm just interested in the terminology here.
Yeah I had forgotten what call by sharing was until I read your post. I'd gotten used to calling it "call by reference"! I think Python uses the same model if I'm not wrong. Anyway, welcome to DEV!
Thanks! 😄
I tried to search any academic source on "call-by-sharing". The only thing I found so far is the manual for theta.
Do you know example of a language which has mutations and uses call-by-value?
I had trouble finding academic resources on "call by sharing", too 🤷♂ C uses call by value and has mutations
I'm still kinda confused.
So if JavaScript uses pass by sharing, but pass by sharing resembles pass by reference, whats the difference?
You mention there is "copy" mechanism when passing the object to the function, which I guess becomes the new "reference" of the object?
Do you have an example where JavaScript would work differently if it uses pass by reference? Or is this all how the JS engine actually handles stuff under the hood?
Okay so when JS passes and argument to a function you get something like this
bar
we can think of as a piece of paper saying where to find the data.Then when we give bar to the function
foo
JavaScript photocopies that piece of paper and gives it to us. If we alter the paper it won't change anything about what's at bar's location. egBut if we go to the address and make changes there other people can see it.
Now because we always make a photocopy no matter what we pass to a function, if we pass a number
This time it's like we wrote the number 2 on a paper
other
and then photocopy it and give it tosomething
If we change the number we are only changing our copy
So if a language is pass by reference what are we doing? We are handing the function that same piece of paper and NOT making a copy of it. And when we are done we give it back to whoever called us. So any changes we make to the paper gets reflected outside the function.
PHP has this functionality
After we call
bar
in the PHP example$foo
will be3
for EVERYONE not just locally in the functionbar
.Hopefully that makes sense.
I think this post is mildly confusing and misleading.
For something super in depth
JavaScript, Ruby and C are not call by reference
Derk-Jan Karrenbeld ・ Jun 27 ・ 9 min read