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
const
overlet
orvar
? 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
var
vslet
andconst
var
is function scoped.let
andconst
are block scoped. This is the biggest difference, to use the wise words of a work colleague: "let
is whatvar
should have been from the start."That said the main difference with
let
vsconst
is thatconst
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 aconst
.I hope I answered your question sufficiently.
Cheers!
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.The scoping component of
let
seems pretty beneficial.let
andconst
seem 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
let
andconst
usage. I opened an issue and I am welcome to PR'sAgain thank you for reading and asking a question.