0

I am new to JS , I am trying to execute the following code and log the out put

var name ="xyz";
var age =10;
var location ={office:"US",home:"CA"};

function callFn(name,age, location){
    name = "abc";
    age =20 ;
    location['office']="new office location";
}
callFn(name, age, location);
console.log(name, age, location['office']);

The output is "xyz", 10 , "new office location";

Why the name , age values are not chaning but key values inside object are changing ? IN other languges like java it will assign new values to name and age also

Roster
  • 1,764
  • 6
  • 17
  • 36
  • 1
    `name` and `age` are primitive types. They are passed to the function by value. While `location` is an object which is passed by reference. Means that `location` which is local variable to the function will refer to the global variable `location`. – Maheer Ali Jun 15 '19 at 09:52
  • @Maheer Ali , Thanks alot to the explanation and link – Roster Jun 15 '19 at 09:54

0 Answers0