Bugs Bunny
Bugs Bunny

Reputation: 2674

Passing ECMAScript 6 const as Function Argument

I've been reading a lot about ES6 lately and decied to give it a try (using Babel). I'm a little confused with new variable declarations let and const.

I understood how scope differs from var; and that const is a permanent reference to a value (and not a constant value itself!).

Q: What if I pass const variable to a function call? Will the receiving function be able to change the value / reference? Why? Why not?

Upvotes: 7

Views: 5029

Answers (3)

Zanon
Zanon

Reputation: 30710

When you pass a variable with a primitive type (string, number, etc.) as argument, you pass the variable by value. When it's a reference to an object or array, you pass by reference, but the function argument is another variable, not const (mutable), that has the same reference.

It means that the argument variable has the same reference and can make changes (like adding or removing data in the array) that will impact the original variable, but if the argument variable changes it's reference (like argument = {}), the changes will not reflect to the original variable (even if the original one is not const).

Example:

const str = 'string';

function changeToNumber(argument) {
  argument = 10;
}

changeToNumber(str);
console.log(str); // 'string'

const obj = { prop: 1 };
let notConstObj = { prop: 1 };

function changeToArray(argument) {
  argument = [10];
}

changeToArray(obj);
console.log(obj); // '{ prop: 1 }'

changeToArray(notConstObj);
console.log(notConstObj); // '{ prop: 1 }'

function modifyObj(argument) {
  argument.prop2 = 10;
}

modifyObj(obj);
console.log(obj); // '{ prop: 1, prop2: 10 }'

Upvotes: 0

user663031
user663031

Reputation:

The semantics of a parameter being passed to a function is completely independent of how that parameter was declared (whether using var, let, or const). What is passed to the function is merely some value. The receiving function has no visibility into how or where the value was derived, including, if it is a variable, how that variable was declared. It merely sees the value that was passed in. The new let and const keywords have no relevance to this behavior.

It is the source of some confusion that given the value of an array or object, that array or object can be manipulated. So I can do the following:

const a = {};
a.x = 1;

const in the above makes a itself a constant within its scope, so it cannot be re-declared or re-assigned, but it in no way prevents the internal structure of a from being changed, in this case by adding a property x.

Similarly, in a parameter-passing context:

function func(obj) { obj.x = 1; }

const a = {};
func(a);

Here, func receives the value of a as obj, but with that value it may access/change the internals of the object.

Of possible interest: How to make function parameter constant in JavaScript?.

Upvotes: 7

It-Z
It-Z

Reputation: 2000

As you can see here and here that the const declaration creates a read-only named constant i.e. not a named reference.

Now, if you pass a const to a function you are actually creating a new scope:

You are passing the const "by value" and the local function variable will get the value of the const and not the reference to it as in any non immutable object (like strings). You will be able to change the local var but not the original const.

Upvotes: 4

Related Questions