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)?
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.
Great write-up! What are your opinions on using
constoverletorvar? What are the actual advantages of it (other than telling the developer that it will not change)?Hey Patrick!
Thanks for the great question. I will try to answer by breaking it down first with
varvsletandconstvaris function scoped.letandconstare block scoped. This is the biggest difference, to use the wise words of a work colleague: "letis whatvarshould have been from the start."That said the main difference with
letvsconstis thatconstdoes 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
letand never change or re-declare it, the error will be to use aconst.I hope I answered your question sufficiently.
Cheers!
consthas a great advantage: there are less moving parts in your code.I now default to
constunless I know the variable is going to be reassigned later on.The scoping component of
letseems pretty beneficial.letandconstseem to remove some of the wishy-washiness of JS.I totally agree. Block scoping is great progress
Patrick,
I was re-reading my post and noticed I am inconsistent with my
letandconstusage. I opened an issue and I am welcome to PR'sAgain thank you for reading and asking a question.