Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Lexical & Dynamic Scope" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle contrasts the models of lexical scoping versus dynamic scoping and gives examples of each.

Preview
Close

Transcript from the "Lexical & Dynamic Scope" Lesson

[00:00:00]
>> Kyle: Lexical scope, that's what we've been talking about, we're gonna give it that formal name now. That is what it is referred to, this idea of scopes being nested within each other. And specifically the idea that a compiler, a parser, a processor is figuring out all those scopes ahead of time before being executed, that's what we mean by the concept of lexical scopes.

[00:00:20]
That's where that name even comes from, that lex shares the same root as the first stage of parsing, the lexa. So think of lexical scope, when you hear that term, think of that as related to the compiler, related to, in essence, the author time decision. Or it's the decision I made at author time to put this function inside of this one and have this variable that's gonna end up being this marble color.

[00:00:48]
It's decided at compile time. It's not decided at run time, it is not affected in any way, shape, or form by what happens in the program.
>> Kyle: That is the first, and I would argue definitely the most predominant, model for scoping among programming languages. In fact, the vast majority of all programming languages in existence, and almost certainly all programming languages that you've ever worked with, are in fact lexically scoped.

[00:01:15]
Bugs me sometimes when people say JavaScript isn't lexically scoped because of some of the nuances of JavaScript's behavior. It is absolutely, definitely, 100% lexically scoped. There's no question about it. There is, however, another model for scoping. It is not very common at all, you generally only see this in a few old academic languages and maybe some different modes.

[00:01:40]
Probably the most predominant example of the dynamic scoping model is actually Bash script. If you've done any significant amount of scripting in Bash, it is actually dynamically scoped rather than lexical scoped. And that fits a bit with our intuition earlier that Bash script is one of those interpreted languages, it's not one of those compiled languages.

[00:02:01]
So it sort of makes sense that it doesn't have a compile time scoping model since it's not compiled, okay? So there's a match in our intuition there. So let's make sure we put a strong definition, or a strong idea around this lexical scope. The intuition in our head is that when we think about lexical scope, we think about something that is fixed at author time and it's predictable, it is not affected by run time conditions.

[00:02:31]
The name dynamic scope implies the opposite, right? It implies that the run time conditions, the dynamic conditions of your program, are going to affect something about the scoping.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now