DEV Community

Discussion on: Notes on ECMAScript 6 (ES6)

Collapse
 
pwaivers profile image
Patrick Waivers

Great write-up! What are your opinions on using const over let or var? What are the actual advantages of it (other than telling the developer that it will not change)?

Collapse
 
hardy613 profile image
Scott Hardy • Edited

Hey Patrick!

Thanks for the great question. I will try to answer by breaking it down first with var vs let and const

var is function scoped. let and const are block scoped. This is the biggest difference, to use the wise words of a work colleague: "let is what var should have been from the start."

That said the main difference with let vs const is that const does not allow re-declaring. This does tell a developer that it will not change as you mentioned, but more importantly, it is telling your program that it cannot change.

Some linters out there (AirBnB) will throw an error if you declare a let and never change or re-declare it, the error will be to use a const.

I hope I answered your question sufficiently.

Cheers!

Collapse
 
flaviocopes profile image
flavio ⚑️πŸ”₯

const has a great advantage: there are less moving parts in your code.

I now default to const unless I know the variable is going to be reassigned later on.

Collapse
 
ben profile image
Ben Halpern

The scoping component of let seems pretty beneficial. let and const seem to remove some of the wishy-washiness of JS.

Collapse
 
hardy613 profile image
Scott Hardy

I totally agree. Block scoping is great progress

Collapse
 
hardy613 profile image
Scott Hardy

Patrick,

I was re-reading my post and noticed I am inconsistent with my let and const usage. I opened an issue and I am welcome to PR's

Again thank you for reading and asking a question.