JS Pass by Value vs Pass by Reference

Jordan Martin
2 min readOct 9, 2018

--

Being an Object-Oriented programming language, almost all elements, including functions, in JavaScript are Objects. The only components that are not Objects are the Primitive Data Types: string, number, Boolean, null, and undefined. These Primitive Data Types also have the characteristic of being immutable, meaning that they cannot be changed after being created. One of the major differences between Objects and Primitive Data Types is that Objects are Pass by References and Primitive Data Types are Pass by Value.

What is Pass by Value?

The easiest way to think about pass by value is that it is making a copy of the original. This means that two of the exact same things are being made. So if you go to change the copy, it does NOT change the original.

For Primitive Data Types, the =operator functions as Pass by Value.

What is Pass by Reference?

Pass by Reference is a little more complicated to understand. Instead of making a copy of the original, you are making a reference to the original value. So a change made on the reference is actually changing the original.

For Objects, the =operator functions as Pass by Reference.

What is the Significance?

The significance here is memory. Every variable is stored in memory and alotted a specific amount of memory. Larger data types, like Objects, require more memory. In Pass by Reference, JavaScript is attempting to save memory space by passing an address in memory (aka a reference) of where your data is instead of allotting additional space in memory to make a separate copy of your data.

Reference:

https://hackernoon.com/grasp-by-value-and-by-reference-in-javascript-7ed75efa1293

https://www.mathwarehouse.com/programming/passing-by-value-vs-by-reference-visual-explanation.php

--

--