Javascript uses call by sharing for objects
Tags: javascript, todayilearned, top7
Published: 2019-09-27 17:53:50

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:  

{% gist https://gist.github.com/ufko/0c9a0a7b7ce14bbcb16ddffc4bc622b5 %}


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.

-----------



###### Cover Photo by Hans-Peter Gauster on Unsplash