Pass by value

JS passes ALL arguments to a function BY VALUE

Any change that you make to the arguments inside the function DOES NOT affect the passing variables outside of the function.

In other words, the changes made to the arguments are not reflected outside of the function.

Passing Primitives

function square(x) {
    x = x * x;
    return x;
}
var y = 10;
var result = square(y);

// what would these print out?
console.log(y);
console.log(result);

Resource: https://www.javascripttutorial.net/javascript-pass-by-value/

Passing Objects

  • When you pass an object to a function, you are passing the reference of that object, not the actual object.

  • Therefore, the function can modify the properties of the object via its reference.

  • BUT the function CANNOT change the reference variable to reference another object.

Last updated