Check out a free preview of the full Deep JavaScript Foundations, v3 course
The "Kinds of Emptiness" 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 dissects the various ways something can be empty in JavaScript: undefined, undeclared, and uninitialized.
Transcript from the "Kinds of Emptiness" Lesson
[00:00:00]
>> Kyle Simpson: A quick note on undefined versus undeclared. This is often confused. And it's confused because developers, quite naturally, think of these words as sort of synonyms. They seem like they mean kind of almost the same thing. And in English, that's probably true. But in programming, it's entirely not true.
[00:00:23]
In programming, and especially in JavaScript, these are two entirely different concepts. So on the previous slide, when I said type of V, and I got back quote, undefined, V or whatever that variable is, didn't even exist. So how is it that I can get back quote undefined when something doesn't even exist?
[00:00:43]
Well, that's another historical wart, JavaScript trying to pretend as if the absence of a declaration isn't that big a deal. It's not that big of a problem, you can work around it. In retrospect, they never should have done that. They should have just returned a string undeclared. Undeclared means it's never been created in any scope that we have access to.
[00:01:04]
And undefined means there's definitely a variable, and at the moment, it has no value.
>> Kyle Simpson: Very different things.
>> Kyle Simpson: The type of operator is the only operator in existence that is able to reference a thing that doesn't exist and not throw an error.
>> Kyle Simpson: Now, undefined and undeclared, we can fairly straightforwardly sift those out.
[00:01:28]
But there's another concept of emptiness in the language that was introduced with, essentially, ES6. And the best way to describe this is uninitialized. Another way of referring to this particular condition that a variable can be in is called the TDZ, the temporal dead zone. We're gonna get much more into this in our next unit of discussion when we talk about scopes.
[00:01:53]
We're gonna talk about where this thing comes from. But essentially, the idea is that certain variables, like block scope ones, don't get initialized. They never initially get set to undefined. When something is in an uninitialized state, it is off-limits. You're not allowed to touch it in any way, shape or form, or you'll get an error, and the error you get is the TDZ error.
[00:02:15]
So it's yet another concept that we have of this emptiness. We can have a variable that's never been initialized. We can have a variable that's been initialized that is undefined. Or we can have a variable that was never even created, and then it's undeclared. Three different concepts that we need to wrap our brains around.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops