DEV Community

Declaring JS Variables in 2019

Sam Thorogood on June 05, 2019

tl;dr my opinion 🤔💭: use const by default, only use let if required. History JavaScript has three* ways to declare variables: var x ...
Collapse
 
simevidas profile image
Šime Vidas • Edited

I’m in the “preventing re-declaration doesn’t really matter, and let is two characters shorter than const, so…” camp.

People have suggested const over let before, but I’m unconvinced. What’s so bad about let being re-declarable? I’ve been using var for years and I don’t think that I’ve ever had a problem specifically with it being re-declarable.

Collapse
 
samthor profile image
Sam Thorogood

Do you mean mutable, vs re-declarable?

var is re-declarable in that I can write var x = 123; var x = 456; and that's totally valid. I suppose let is re-declarable in a lower scope (inside {}).

Collapse
 
simevidas profile image
Šime Vidas • Edited

Ah yes, I meant re-assignable, not re-declarable.

The argument for const over let is that const “protects” you from unintended reassignment, but I don’t see that as a problem that requires extra measures.

let everywhere is simple. const and let together is more complex, so there needs to be a good reason for the added complexity to justify it. I don’t like making my code more complex just because something sounds like a cool feature.

People who argue for const haven’t convinced me yet that accidental reassignment is something that I need to guard against.

I hope that makes sense.

Thread Thread
 
samthor profile image
Sam Thorogood

Would you use const for say, defining magic numbers at the top of your program?

const POST_TIMEOUT = 60 * 1000;


// ... much later
function blah() {
  setTimeout(foo, POST_TIMEOUT);
}

Just trying to understand if it fits into your world at all :)

Thread Thread
 
simevidas profile image
Šime Vidas

const is most likely is a very useful feature in certain situations. But what are those situations? I can only guess. I personally don’t think that I need const because I don’t think that accidental reassignments are an issue in my own code.

So what are those situations? That’s what people arguing for const should focus on explaining, in my option.

I think I have a good analogy. In CSS, I argue for using this code:

[hidden] {
  display: none 1important;`
}

The !important part is similar to const. It protects the display property from being re-assigned if the hidden attribute is present. Why is that important? Because a <div class="foo" hidden> element could get accidentally rendered with something that is as simple as .foo { display: flex; }. That .foo style may not have intended to style that specific hidden element but it did match it, and now the hidden attribute is ignored and the element is rendered.

So I think I make a good case for adding !important in this particular situation.

I would like someone to make a good case for why POST_TIMEOUT needs to be const. What concrete problem can be prevented by that?

Thread Thread
 
richiksc profile image
Richik SC

It's about two things: semantics - code is not just meant for the computer, it's also for humans who read it. Using const better signifies to team members and maintainers that this variable shouldn't change. Another is safety by default. Yes, maybe your code doesn't have issues with constants. But it's about baking in safety to your code to reduce the possibility of any errors. It's like how Java class variables are package-private by default (like @flame10a said) and it's highly recommended to make them private, and variables in Rust are actually immutable (non-reassignable) by default. So, in the future, if for example you're coding while running low on sleep, or one of your coworkers decides to work on it and he hasn't worked on the project before, you are a lot less likely to make a mistake.

Collapse
 
flame10a profile image
Flame10A

I very much agree with 'const first'; it's in the same realm as Java class variables being private by default - no matter how safe your code may be, there's no harm in it being that bit safer!

let is of course good for any local variable that may change within its scope, but otherwise it's safer (e.g. in case of autocomplete shenanigans, or plain misunderstanding), and more visually meaningful, to declare a variable as unchangable.

On the odd case of function declarations... I'm very much against using the function foo() { ... } syntax out of place; much like var, the funcionality of it can be rather backwards, and in turn can be confusing to readers of any level.

Collapse
 
aprouja1 profile image
aprouja1

You also need to remember that let and const have block scope:

let i = 0;
console.log(1,i);
if(i===0){
  let i = 1;
  console.log(2,i)
}
console.log(3,i)

This will result in a log of

1 0
2 1
3 0
Collapse
 
samthor profile image
Sam Thorogood

Actually, not mentioning this was one of the goals of my article. I think this behavior, for new programmers, is logically the most sensible!

(Having a comment pointing it out is fine though 😅)

Collapse
 
jimbotsov profile image
JimboTSoV

I disagree regarding const. Spamming the keyword inflates it's meaning to me. I believe you should only use it when you actively intend a variable to be unchangeable - because otherwise when reading another person's code I would conclude you just used the keyword without thought and disregard const as a whole.
I do agree with the rest of your post in general though. Instead of using constants as a default, I'd recommend to rather put thought into your code before deciding which keyword to use.
But to quote you, this is just my opinion piece 😉

Collapse
 
harsh183 profile image
Harsh Deep

I wish it used different keywords from const vs let. Like Kotlins val vs var setup.

Collapse
 
samthor profile image
Sam Thorogood

I think there's a high number of people who don't like const purely because it's two letters longer, yeah. Again, this is opinion, so... ¯\_(ツ)_/¯