Skip to Content
JavaScript
book

JavaScript

by T. J. Crowder
July 2020
Intermediate to advanced content levelIntermediate to advanced
608 pages
15h 43m
English
Wrox
Content preview from JavaScript

2Block-Scoped Declarations: let and const

In this chapter you'll learn how the new let and const declarations work and what problems they solve. Throughout the chapter, you'll see some behaviors of var that have proved problematic, and you'll learn how let and const solve those problems. You'll see how let and const provide true block scope and prevent confusion caused by repeated declarations or by using a variable before you initialize it. You'll discover how block scope means you can use let to avoid the traditional “closures in loop” problem, and how const lets you create constants: “variables” whose values cannot change. You'll learn how let and const avoid creating even more properties on the already-overburdened global object. In short, you'll learn why let and const are the new var, and why var no longer has a place in modern JavaScript programming.

AN INTRODUCTION TO LET AND CONST

Like var, let declares variables:

let x = 2;
x += 40;
console.log(x); // 42

You can use let anywhere you can use var. Just as with var, you ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced JavaScript

Advanced JavaScript

Zachary Shute

Publisher Resources

ISBN: 9781119367956Purchase book