Skip to main content
Commonmark migration
Source Link

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

 

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter and the function changes the object's properties, that change is visible outside the function. Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value. It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

 

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter and the function changes the object's properties, that change is visible outside the function. Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value. It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter and the function changes the object's properties, that change is visible outside the function. Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value. It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

added 541 characters in body
Source Link
Code Maniac
  • 37.9k
  • 5
  • 44
  • 65

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter and the function changes the object's properties, that change is visible outside the function. Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Well in javascript objectsobjects are passed by referencereference so when you pass a object to a function you're passing it's memory referencememory reference not a copycopy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive valueprimitive value it is passed by valuevalue.It It passes a copycopy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value.It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter and the function changes the object's properties, that change is visible outside the function. Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value. It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

Source Link
Code Maniac
  • 37.9k
  • 5
  • 44
  • 65

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value.It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);