Do the Arguments in JavaScript Are Passed By Value Or Reference?

Since Java is a complicated language, beginners may find developing Java Web Application Development or web pages a difficult task. JavaScript (also referred to as JS) is much easier compared to Java, and helps to enhance how your web page works. Incorporating JS into your site's web page helps in improving visitor's experience.

JavaScript beginners may find themselves perplexed over whether JavaScript parameters are “Passed by Value” or “Passed by Reference”. To obtain answers to this question continue reading this article further.

JavaScript: Passing by Value or Reference

All the arguments in JavaScript – the primitive types like string, number etc. – are always passed to the function via the “Pass by Value” method. However, in case of objects and arrays the values are passed using the “Pass by Reference” method.

Let us take a look at an example to illustrate the above discussed point in a better manner. In the following snippet a object is passed to a function:

function mutate(ob){
var info={x:5,y:10};
console.log(info.x); //changed to 15

Looking at the above code chances are that you will be wondering if arguments in JavaScript are always passed by value, then – how the change to the property of object is propagated outside the function, similar to the original object? That's because the reference of the object is passed by value to the mutate() function.

Prior to calling the mutate() function, our variable “info” contains a reference to the object {x:5,y:10}. On calling the function mutate and using info as an argument, the reference to our variable ob is passed by value. And so, both our variables “info” and “ob” hold reference to the same object {x:5,y:10}. And as a result, whenever a change to the object property is made via the reference obj, it gets reflected in the original object.

By assigning “null” value to the reference ob, it won't no longer point to the original object. However, the link between info (the object reference) and ob (the object) will remain the same. Below is a code snippet that sets the value of object to null.

function assign(ob){
ob=null; //now ob doesn't refer to the original object
var info={x:5,y:10};
console.log(info.x); //changed to 15

This means that when you set the ob (the object reference) to another value it won't have any effect on the variable info, which is passed as an argument to the assign() function. But in case you mutate some property of the object, the change will be reflected in the original object.

What Happens in Case of Primitives?

Let us look at a code snippet using primitives types:

function xyz(num){
console.log(num); //15
var num1=10;
console.log(num1); //10

Now the changes that will be made within the xyz() function won't propagate outside the function. That's because the variable num1 is passed by value. Moreover, the changes are made to the copy of the argument num.


Hopefully, reading this article will help you understand that arguments in JS “is always passed by value”. And you can't pass an object to a function, and instead a reference to the object is passed.

Source: Java Development Services Provider

Celin Smith Celin Smith is Java developer and blogger at Xicom who loves to write about web & mobile apps. Xicom is a one of the leading Java Development Outsourcing Company which provides different array of genuine software solutions like Custom Software Development, IT Outsourcing Services, Web application development services to its clients globally. Xicom Technologies Ltd., a software development realm, is a CMMI Level-3 & ISO 9001 Certified company that is renowned for providing top-notch services to its global clientele.
  • java development services provider
  • java web application development
  • j2ee application development
  • hire java developers
By Celin Smith On 23 Dec, 14  Viewed: 195

Other blogs you may like

Uncovering key differences between IdentityHashMap and HashMap in Java

As a concurrent, object-oriented computer programming language, [Java web development][1] has been incredibly embraced by web programmers all over the world. With innovative features and functionalities getting added to Java on a regular basis, it won't be wrong to say that the software product is... By Celin Smith   On 26 Dec 2014  Viewed: 207