Loading topic
var, let, and const. They look almost the same on the page, but they behave differently in ways that cause real bugs. This chapter covers the basics: the syntax of each keyword, what happens when a variable is created, and the terms the res6 min readvar, let, and const differ, you first need a clear picture of what scope is, how scopes nest, and how JavaScript finds a6 min readif statements and for loops, and run code in a browser console or Node.js. It also assumes you have read the previous chapter's description of global, function, and block scope. Here we look closely at one rule: **a var declaration bel6 min readif statements, and for loops. It also assumes you have read the previous chapter on var and function scope. Hoisting, the temporal dead zone, and the loop-closure bug each have their own chapter later. They appear here only where they affect how bl5 min readif statements and for loops, and run code in Node.js or a browser console. It also assumes you have read the earlier chapters on function scope (var) and block scope (let and const). Hoisting only makes sense once you know which sc6 min readtry...catch, and read a ReferenceError in the console. It also builds on the previous chapter. There you saw that let and const declarations are hoisted, but reading one before its declaration throws instead of returning undefined.7 min readvar, let, and const, and that you know the difference between function scope and block scope from the earlier chapters. You should also be comfortable with try...catch and running code in Node.js or a browser console.6 min readlet, const, and var are declared, and that assigning a new value to a const throws a TypeError. It also assumes you are comfortable with objects, arrays, and strict mode at the level of "it's a mode that make5 min readfor loops, arrow functions, and setTimeout, and that you have read the earlier chapters on function scope (`va7 min readvar, let, and const, as covered in earlier chapters. Here we look at what happens when you declare variables outside of every function and block, at the very top of a file. The answe7 min readvar, let, and const are scoped, and how top-level var in a classic script becomes a property of globalThis (covered in the previous chapter). You should also be comfortable with functions, try...catch, and running code in Node.js or a browser console.6 min readvar, let, and const behave. This chapter turns those rules into a practical habit. It assumes you have read them and can explain block scope versus function scope, the temporal dead zone (TDZ), the difference between a const binding and an immutable value, th7 min read