Is array.push a form of reassignment?
Asked Answered
A

2

15

I'm working in a project with several javascript files and part of what I'm doing right now is both migrating existing code to use newer ES6+ features as well as making sure we're abiding by the AirBnB Eslint Rules.

So, given that context, this is the specific situation:

let meta = [a.platform];

And right below that:

meta.push(a.browserName ? a.browserName : 'any');

So now the linter is giving me a warning: 'meta is never reassigned. Use const instead'.


I understand that meta = somethingNew would be reassigning the variable. But in this case, isn't this variable also something different than what it used to be when it was created?

or, to make it even more clear

Can I use a const to define an array that will receive new items? If not, why?

Also, if not: why is the linter throwing a warning?

Agglomerate answered 9/8, 2017 at 16:23 Comment(3)
Reassignment of Object types (like Arrays) just means to rebind a name with another reference (to another Object type). When you mutate an Object type as in your example, you merely change its properties, but not the underlying reference.Afterdeck
"But in this case, isn't this variable also something different than what it used to be when it was created?" No. The variable still has the same array as value. The arrays was mutated in the meantime, but that doesn't make it a different value.Magnifico
If you worked with C language, might help to think of the const object (array, type etc) as an address pointer to a memory block. That const pointer address is fixed/mutated , cant be reassigned another const object. but the data in the memory address that the const object points to - can be amended.Sestet
A
11

The only thing you have to know is that const has nothing to do with immutability. const simply allows you to prevent reassignment, which means that you cannot do that:

   
// OK 
const foo = '';
const bar = [];
const baz = {};

// WTF are we doing!?
foo = 'Foo';
bar = ['Bar'];
baz = {baz: 'Baz'};

However, in JavaScript, object values are mutable ; contrary to primitive values that are immutable. So if you put a string in a const, you will not be able to modify it at all, even with String.prototype methods (which return new strings).

const str = 'Lorem ipsum';

console.log(str.slice(6)); // This is actually a new string...
console.log(str); // The original string has not been modified!

It is quite different with arrays or object literals, though:

const arr = [];
const obj = {};

arr.push('Foo');
arr.push('Bar');

obj.foo = 'Foo';
obj.bar = 'Bar';

console.log(arr); // Modified!
console.log(obj); // Modified!

At this point, it should be clear: the linter shows this warning because, indeed, meta is never reassigned... A mutation is not a reassignment.

Anticipant answered 9/8, 2017 at 17:22 Comment(0)
N
6

meta.push() doesn't reassign the variable, it just modifies the array that the variable refers to -- it's still the same array, but with different contents. So you can use const for the variable declaration, since you never make it refer to a different array.

The same is true if you assign to array elements. This is OK:

const meta = [a.platform];
meta[1] = somethingNew;

The second assignment doesn't change meta, it changes the array.

Notebook answered 9/8, 2017 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.