Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred declaration convention for objects or arrays: const or let?

I'm not asking what's technically possible; I know you can do

const a = [];
const b = {};
a.push['sup'];
b.test = 'earth';

What I'm wondering is whether there's any convention for preferring let over const when it comes to arrays and objects that will have their internals modified. If you see an object declared with const, do you assume the intention was for the object to be immutable, and would you have preferred to see let instead, or, since some linters (like tslint) have a problem with that, is it better just to declare it with const and trust that anyone else reading the code knows that that doesn't mean it's immutable?

like image 464
redOctober13 Avatar asked Oct 03 '17 20:10

redOctober13


People also ask

Should I use const objects?

Always declare a variable with const when you know that the value should not be changed. Use const when you declare: A new Array. A new Object.

Can const be declared?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).

What does const {} mean in JavaScript?

In JavaScript, `const` means that the identifier can't be reassigned. (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable. js and Mori, a `const` object can have properties mutated.)

When to use let and const in JavaScript?

Summary. As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable.


2 Answers

The const keyword in front of an object implies that there is an object, and you're working with references to alter it. It also (correctly) implies that you should not attempt to reassign references to this object.

const obj = {a: 'foo', b: 'bar'};

const obj2 = {z: 'baz'};

obj = obj2; // const will prevent this operation. 

const does not imply that the object properties should not be altered. It does imply that you should not try to change the reference.

If you plan to reassign references to the object, then you use let.

Source: AirBnB Javascript Style Guide

like image 150
LennieCodes Avatar answered Oct 12 '22 08:10

LennieCodes


There is no preferred one, its based on your choice of usage for that array or object. You have to understand mutation and reassigning clearly.

Mutation - updates the values present in the memory

Reassign - variable points to new memory locations where new values are stored

Let - offers both mutation and reassiging

Const - offers mutation but not reassiging

Both - doesnot offers redeclaring

If your usecase only needs mutation, you can go for const.. if you need reassigning then go for let.

// LET

let condiments = ['Ketchup', 'Soy Sauce', 'Sriracha'];

// Mutation possible
condiments[0] = 'Mayo';
console.log(condiments);//=> [ 'Mayo', 'Soy Sauce', 'Sriracha' ]

// Re-assigning possible
condiments = ['Mayo'];
console.log(condiments); //=> [ 'Mayo' ]

// Re-declaring not possible
//let condiments = [] //=> SyntaxError: Identifier 'condiments' has already been declared


// CONST

const utensils = ['Fork', 'Chopsticks', 'Spork'];

// Mutation Possible
utensils[2] = 'Spoon'
console.log(utensils); //=> [ 'Fork', 'Chopsticks', 'Spoon' ]
utensils.length = 0
console.log(utensils); //=> [ ]

// Re-assigning not possible
//utensils = ['Spoon']; //=> TypeError: Assignment to constant variable.

// Re-declaring not possible
//const utensils = {} //=> SyntaxError: Identifier 'condiments' has already been declared
like image 35
JS-JeevaSaravanan Avatar answered Oct 12 '22 08:10

JS-JeevaSaravanan