Javascript Intro to Pass by Value vs Pass by Reference

Asif Khan
2 min readDec 16, 2020

--

Before we get into pass by value or pass by reference it is key to understand primitive data type. Primitive data types are undefined, null, string, boolean, symbol, number. All primitive data types are immutable(which means values can change once created). Now that primitive data types have been slightly introduced we can look at an intro to pass by value vs pass by reference.

Pass by Value:

When used in assignments primitive data types pass only their value.

Primitive data types only pass by their values, so what you have written “above” is the same as:

You can tell that `a` and `b` are not actually related in any way. `a` and `b` are initialized to the same value, but point to a different memory location.

Pass by Reference:

Reference types save a “pointer” or a reference stored elsewhere in memory. As a result, assignments actually do link to each other.

Both variables are referencing the same object, a change in one means a change in both. Additionally, you may use the spread operator to make an unrelated copy. For example:

--

--

Asif Khan
Asif Khan

Written by Asif Khan

Currently a Software Engineering fellow at The Marcy Lab school. Chasing my dream to inspire the youth. Really grateful to be sharing my words with you.

No responses yet