</>CodexBash
All articles
Python and JavaScript

var, let and const

The three ways to declare variables in JavaScript compared: function scope versus block scope, hoisting and the temporal dead zone, redeclaration, the classic loop and closure bug, what const really prevents, freezing objects, and the simple rule most teams follow.

CodexBash, , 5 min read

JavaScript has three keywords for declaring variables: the original var, and let and const, added in 2015. They look interchangeable in simple code, but they differ in scope, in what happens before the declaration line, and in whether the variable can be reassigned. Those differences explain a whole family of classic bugs and interview questions. This article walks through each difference with runnable examples, then gives the simple rule modern code follows.

Sign in to read the rest

The whole 5 minute article is free with an account, along with every problem, lesson and tutorial. No card.