Javascript: Always use const to declare variables. ???

Created on 10 Feb 2016  Â·  20Comments  Â·  Source: airbnb/javascript

Shouldn't it be something along these lines :

  • Always use let to declare variables. Always use const to declare constantes or references. Avoid using var. Not doing so will result in global variables...
  • Use one const or let declaration per variable.

Maybe the "references" and "variables" parts should be merged, linked or moved next to each other.

editorial question wontfix

Most helpful comment

I know it's an old thread but I am also wondering the same. Why use constant for everything? For me It's not semantically correct. I tend to follow Kyle Simpson for it. In all the languages I have written programs, constant means a constant such as value of PI. I use const for only constants. I think we are forgetting the semantic aspect of programming. Just because const is available to us, which will stop us reassigning the variable, it doesn't mean we should use const for everything. Maybe it's good for lazy programmer but we are abusing it.

Here is what my typical JavaScript file looks like

const PI =3.4;

function some(){
var someVar = 36; // So I know that this variable is available in all inner scopes of function

if(doSome){
let innerScopevar = 56; //Now I know it's only available in this scope.
}

}

All 20 comments

Nope! Everything should be const, because reassignment is something that should be avoided.

Then they should be called "references" not "variables" anymore.

ah, lol, now I see what you mean. I think that colloquially, the name is "variable" even when the value doesn't vary.

Using the term "reference", I think, is very dangerous in a language that is exclusively pass-by-value, where the concept of "references" is already poorly understood.

I agree that the word "variable" should not be used to describe something declared with const. If we're going to insist on immutability, heck, let's do it.

Perhaps the word "local" will do (not just here but thoughout the doc).

In JS, const does not mean immutable. It simply means "can not be reassigned". const a = {}; a.foo = 3; is perfectly valid.

declaring objects as const is best practice,
but declaring everything as const is something else,
if we have to use variable i in for loop we would not be declaring it as a const.

So, i think what @e-hamon wanted to say was that it's a good practice to declare objects as const but other local variables as let

@nivesh2 that is true, because in a for loop, it would be reassigned. If you used a for..in or for..of loop, you would use const (of course, you're not using loops at all, because that's against the guide, right?)

The best practice is to declare everything as const, only use let when you're forced to reassign the variable (which should be avoided), and _never_ var.

Thanks all, for your replies. Things are getting clearer for me.
Yes, I'm new on ES6, and that "const is a variable" thing puzzled me. I'm glad, I'm not alone.
After carefully reading the style guide again, I think what also got me are a few inconsistencies between the 'types', 'references' and 'variables' sections.
I'll let it brew and maybe offer some editorial fixes in a few days. I guess a pull request is the best channel.

That'd be great, thanks!

This is old issue, but here is my 5 cents. How I use var, let and const in my projects:

  • Never use var (obvious, not needed anymore)
  • Use const for variables that aren't reassigned and mutated
  • Use let for everything else

Examples:

const SOME_CONSTANT = 1;
const SOME_COMPLEX_CONSTANT = {
  a: 1,
  b: 2,
};
// SOME_COMPLEX_CONSTANT is never mutated.

function test() {
  let a = [];
  a.push(1);
  a.push(2);
  // a is not reassigned but mutated.
  const b = [1, 2, 3];
  // b is never mutated.
}

Why? Because that way you can easily see whether some object is mutated during the function execution. With const you can't know that.

Of course you can't distinguish reassignment and mutability with that style, but mutability guarantees are more important in my opinion.

Any thoughts?

const means "constant reference", not "constant value" - as such, mutability is irrelevant. Using let for something that's never reassigned but is mutated furthers developer ignorance and misunderstanding about what const is. Your a there should be a const.

if you want to create a object immutable,you can use method Object.freeze()

  const CONSTANT = {key:1};
  Object.freeze(CONSTANT);
  CONSTANT.key = 2;  //2
  console.log(CONSTANT);   //{key:2}

as @ljharb mention,const means "constant reference"

@WangXiZhu Object.freeze only creates an object that's shallowly immutable - values of arrays, objects, Sets, and Maps won't be made immutable, and even if you Object.freeze a Set or a Map, it can't ever be made immutable.

thanks, yeah, i create an object - values of Sets,it isn't immutable when Object.freeze the Set.
Object.freeze work on the object like {key: 1},but how can i create an Object immutable?
there is a way to use facebook's Immutable.js. thank you again

I know it's an old thread but I am also wondering the same. Why use constant for everything? For me It's not semantically correct. I tend to follow Kyle Simpson for it. In all the languages I have written programs, constant means a constant such as value of PI. I use const for only constants. I think we are forgetting the semantic aspect of programming. Just because const is available to us, which will stop us reassigning the variable, it doesn't mean we should use const for everything. Maybe it's good for lazy programmer but we are abusing it.

Here is what my typical JavaScript file looks like

const PI =3.4;

function some(){
var someVar = 36; // So I know that this variable is available in all inner scopes of function

if(doSome){
let innerScopevar = 56; //Now I know it's only available in this scope.
}

}

@Mujaddadi const means “constant reference”, not “constant value”, so it’s semantically correct for every value that is not reassigned.

If you are using const like “constant value” - even if that’s what it means in other languages (it’s not as many as you’d think), then that’s abuse - you’re not using JavaScript like it’s JavaScript.

When declared at the top of a function, all of var, let, and const have identical scope, so you shouldn’t need var to signal that there.

@ljharb I understand that the const is constant reference”, not “constant value” in JavaScript but that doesn't change my intended purpose of using the const because even if we are defining constant values with const, the JavaScript datatypes other than objects are immutable. So they will emit the same behavior as constants.

I tried to find the reference from ECMAScript specification regarding their intended use for the const but couldn't find it. Perhaps if you can point me towards the right section.

I agree that var, let, and const will have similar scope but that's not the intent use of var there. By using var, I visually mean that let can be used in inner scopes to semantically and also effectively signal that this variable belongs to that inner scope. If will use let also for variables which are required in inner scope, that will not create any visual signal for me.

My pint is, why creating confusion in a language, which has already many confusing parts. People take meaning of const literally and which creates more confusion.

As Kyle Simpson would say, people need to learn how the language works :-)

Creating confusion happens when const is misused to connote “constant value” (perhaps by only using it for primitives), or when “var” is used to mean “has function scope” when let/const, definedin a function scope, also have function scope.

And as Kyle Simpson will ask for the proper reference of using const other than the constants :D

I probably didn't explain properly about my use of var there. Anyway, I think to each his own.

const means “constant reference”, not “constant value” - your intended purpose is irrelevant if it contradicts what the keyword means in the language.

Was this page helpful?
0 / 5 - 0 ratings