DEV Community

Cover image for 🛸 Is JavaScript using pass by value or pass by reference? Let's find out!
Benjamin Mock
Benjamin Mock

Posted on • Updated on • Originally published at codesnacks.net

🛸 Is JavaScript using pass by value or pass by reference? Let's find out!

To gain a deeper understanding of JS we'll have a look at methods and how arguments are passed. There are two ways: pass by value and pass by reference. Let's find out what's used in JS!

Let's have a look at primitive data types or primitive values first.

console.log(typeof "some string") // string
console.log(typeof true) // boolean
console.log(typeof 42) // number
console.log(typeof 42n) // bigint
console.log(typeof Symbol()) // symbol
console.log(typeof undefined) // undefined

You could also add null to the primitive types, but that's a confusing thing. It's an object, even though it should actually not be one. That's why I'll leave it out here.

So let's see how these primitive types are handled when we pass them to a method as arguments:

let x = "dog"

function change(y) {
    y = "cat"
} 

change(x)

console.log(x) // dog

We can see in this example, that x was passed by value. That means, that the actual value of x which is "dog" was passed to the change method. Even though the argument was reassigned in the change method, that didn't change our x.

If JS would use pass by reference not the value, but the reference to the variable would be passed into a method. Reassigned the argument would actually change the reference of the original variable.

Objects are a bit special, though. An object's value is its reference. Sounds confusing? Let's have a look at an example to explain it

function changeMember(obj) {
    obj.name = "Doggo"
}

function changeReference(obj) {
    obj = {name: "Birdo"}
}

const cat = { name: "Kitty" }
changeMember(cat); // {name: "Doggo"}

changeReference(cat)
console.log(cat); // {name: "Doggo"}

You can see, that also objects are passed by value. It's not possible to change the original object by reassigning the argument of the changeReference method. But, and that can be confusing sometimes if you change a property of an object, that was passed as an argument to a method like in changeMember, that change will be reflected in the original object. That's because the value of an object is basically just its reference. When calling changeMember we're not trying to reassign the variable, but we change a property, which also changes the underlying referenced object.


Want to get better at Web Development?
🚀🚀🚀subscribe to my weekly ✉️newsletter

Top comments (4)

Collapse
 
gypsydave5 profile image
David Wickes

Is JavaScript using pass by value or pass by reference?

Neither. It's call by sharing. Same as Python, Ruby and many others.

The evaluation model you (and many others) describe for JavaScript is both inaccurate and overcomplicated. This way of describing it seems to have been chiefly promoted by Java developers (who really do have primitives and have to worry about boxing etc.) when they blog about JavaScript or learn JavaScript. And there's a lot of Java developers, so this complicated myth has drowned out the simple truth.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

When I worked on my Leaf language I called things shared to denote the sharing. It followed this pattern quite closely.