Parameter passing in JavaScript

Murat Catal
Sep 4, 2018 · 2 min read
“HTML & CSS book” by Greg Rakozy on Unsplash

Many programming languages use two different methodology while working with parameters send to functions.

  • Pass by reference which reference of parameters are sent instead of value
  • Pass by value which value of parameter is sent instead of reference

In javascript, the methodology changes according to your data type.

  • Primitive types such as string, boolean or number is sent by passing by values which means your reference is not copied but your value.
  • For object type parameters, pass by reference method is used. Because of your reference is copied, any modification in your function will effect original object.

Let’s try it with below code snippet

How to deal with object references

Object.assign

Object.assign is a new feature of ES6 standarts. Using this function, you can create new an object by copying source object’s properties. Let’s test it by modifying above example.

Lodash

Another useful function which is called cloneDeep can be used to full copy of all properties in an object.

ImmutableJS

Immutable js is a library developed by facebook and solves our most of the problems while working on objects.


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade