Passing ECMAScript 6 const as Function Argument

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?.


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.