All Articles

How to use var, let and const in modern JavaScript for ES6+

var - I don’t use vars in my ES6+ code, because it declares a function-scoped or globally-scoped variable. Also, vars are created with the value undefined before any code is executed (hoisting).

const - My default first choice for most cases, where I don’t plan to reassign the variable identifier. Constants are block-scoped, the value can’t be reassignment or redeclared. Should be initialized. In general, this is a read-only reference for a value. You can’t reassign the object for the constant, but you can change properties for the object.

let - My bet for cases where I need to reasign a variable. Declares variables in the block scope where it will be used. You can optionally initialize a value to it.