As I was reviewing this week’s student projects for the Vanilla JS Academy during the weekly office hours, I noticed that a lot of my students were using
const instead of
If you’re not already familiar with the differences between them, and when and why you’d use them, here’s a quick primer.
The tl;dr version:
let works mostly the same as
var but won’t let you redefine the variable a second time within the same scope, while
const won’t let you ever redefine the variable. They’re a good way to protect your variables from being accidentally changed or overwritten.
But I don’t use them, and a few of my students asked why.
They only work in modern browsers, and IE11 up. And unlike most of the cool new ES6 stuff I share on this site, they can’t be polyfilled.
My preferred development approach is to write in fully native JS and use polyfills to add backwards compatibility. While I do minify and concatenate stuff, I never run my code through a transpiler to transform it from ES6 into ES5, and I don’t want to have to.
const would force me to choose between deeper backwards compatibility and adding more tools (and bloat) to my development process.