Image for post
Image for post

[Javascript] Pass By Value And Pass By Reference In JavaScript

Raghuraman Kesavan
Aug 23, 2017 · 2 min read

In this post, we will look into pass by value and pass by reference in Javascript.

Let’s see what is pass by value and pass by reference before looking into its javascript context.

In Pass by Value, Function is called by directly passing the value of the variable as the argument. Changing the argument inside the function doesn’t affect the variable passed from outside the function.

Javascript always pass by value so changing the value of the variable never changes the underlying primitive (String or number).

However, when a variable refers to an object which includes array, the value is the reference to the object.

In Pass by Reference, Function is called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affect the variable passed from outside the function. In Javascript objects and arrays follows pass by reference.

so if we are passing object or array as an argument to the method, then there is a possibility that value of the object can change.

If you want to create websites or portal — contact us @ https://www.bondesk.in

Bondesk.In

Share your knowledge!

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store