Skip to Content
Learning JavaScript, 3rd Edition
book

Learning JavaScript, 3rd Edition

by Ethan Brown
February 2016
Beginner
348 pages
8h 40m
English
O'Reilly Media, Inc.
Content preview from Learning JavaScript, 3rd Edition

Chapter 7. Scope

Scope determines when and where variables, constants, and arguments are considered to be defined. We’ve already had some exposure to scope: we know that the arguments of a function exist only in the body of the function. Consider the following:

function f(x) {
    return x + 3;
}
f(5);       // 8
x;          // ReferenceError: x is not defined

We know that x lived very briefly (otherwise, how would it have successfully calculated x + 3?), but we can see that outside of the function body, it’s as if x doesn’t exist. Thus, we say the scope of x is the function f.

When we say that the scope of a variable is a given function, we must remember that the formal arguments in the function body don’t exist until the function is called (thereby becoming actual arguments). A function may be called multiple times: each time the function is called, its arguments come into existence, and then go out of scope when the function returns.

We have also taken it for granted that variables and constants do not exist before we create them. That is, they aren’t in scope until we declare them with let or const (var is a special case we’ll cover later in this chapter).

Note

In some languages, there’s an explicit distinction between declaration and definition. Typically, declaring a variable means that you are announcing its existence by giving it an identifier. Definition, on the other hand, usually means declaring it and giving it a value. In JavaScript, the two terms are interchangeable, as all variables ...

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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn JavaScript

Learn JavaScript

Shaun Wassell

Publisher Resources

ISBN: 9781491914892Errata Page