Loading topic
let, const, and var. They look alike, but they follow different rules. This chapter covers the basic syntax of each one and how initialization works. Later chapters explain the deeper differences: scope, hoisting, and redeclaration.2 min readif statements and for loops, and run code in a browser console or Node.js. It explains what scope is and how JavaScript finds a variable when your code uses its name. The details of how var, let, and const each behave come in later3 min readif statements and for loops, and run code in a browser console or Node.js. It also builds on the previous chapter's idea that scope is the region of code where a name can be used.2 min readif statements, and for loops. It also assumes you have read the previous chapter on how var is scoped to the whole function. Here you will see how let and const follow a tighter rule: they belong to the nearest enclosing block.3 min readvar is scoped to the function and that let and const are scoped to the block. You should also be comfortable writing functions and reading try...catch blocks. Here we look at what the 3 min readlet, const, and var are declared, how block scope works, and that declarations are hoisted (registered when a scope starts) before any code in that scope runs. The hoisting chapter showed that let throws an error where var gives undefined. This chapter e3 min readvar, let, and const, and that you know the difference between function scope and block scope. Here you will learn two separate rules that people often confuse:3 min readconst variable cannot be reassigned. Many developers take that rule to mean the value stored in a const can never change. That is wrong, and the mistake caus2 min readvar is function-scoped and how let and const are block-scoped (covered in earlier chapters). You should also be comfortable writing arrow functions and calling array methods such as map.3 min readvar, let, and const differ in scope, and that you can run code in a browser console or the Node.js REPL. It covers one more difference between the keywords: what happens when you declare a variable at the top level of a script, outside any function or block.3 min readvar, let, and const are scoped, and that a top-level var in a classic script becomes a property of globalThis. Here we look at a related trap: creating a global variable without declaring it at all.3 min readconst bindings, loop closures, and globals. It does not re-explain those rules. It turns them into one decision you can make every time you declare a variable.3 min read