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?
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.
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).
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.)
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With