var). Nowadays I always reach for
const unless I'm forced to.
const) over a variable (
When am I forced to use
const x = "hi"; x = "bye"; // VM226:1 Uncaught TypeError: Assignment to constant variable
This way I don't bother with thinking about which declaration I should use, I'll be alerted when I have to by following a rule.
Arrays and Objects
In this case I still use
const even if changes are being made.
const arr = ; arr.push(1);
let arr = ; arr = [...arr, 1];
In this case I will switch my
const to a
const over let unless I'm forced to
I just let my tools let me know if a variable is necessary. It also makes sense to use a variable (
let) when the value stored inside of it varies throughout the lifecycle of the program.
Top comments (4)
Thanks for pointing that out. I just made an edit to the post to address this.
I still would recommend using
What's most important is for the team members to be on the same page and use linters if possible to codify the understanding.
var is underrated.