One should not take the precise definition of 'undefined'
too non-nonchalantly as it forms an important basis for understanding the JavaScript fundamentals. I'd be curious to know how many so-called expert JavaScript developers really understand what it is. In my many years of learning the ins and outs of the JavaScript programming language, this is perhaps the best explanation of 'undefined'
that I've ever heard.
"When I declare 'var a'
, 'a'
is placed into memory during the creation phase. So the execution context saw 'var a'
and setup 'a'
in memory. And so even though I haven't set it to a value, the JavaScript engine, which is doing more than what I'm just writing in my code, already set it to the special value called 'undefined'
. So 'undefined'
is not like empty, or doesn't exist, it doesn't literally not exist. It's actually a value, it's actually taking up memory space. It's a special keyword, a special value that means this is the value that was initially set by JavaScript. And that leads to a little bit of a warning... Never set yourself a variable equal to 'undefined'
. Because actually you can... That's perfectly valid JavaScript, but it's a little dangerous. It's better to let 'undefined'
, that special keyword, mean I, the programmer, never set the value..."
"... That will really help you when debugging code. If you make a habit of setting values equal to 'undefined'
, then it's really hard to tell if something is 'undefined'
because you set it or because the JavaScript engine set it and you never set it to anything else. It's always better to let 'undefined'
mean I never set this value. That's really useful, and it will help you in your debugging. So 'undefined'
, this is a special value, that is also a special keyword in JavaScript, and it's the value that variables receive during the creation phase, the first phase of creating an execution context, sets up the memory of the variable, and in that memory space puts the value called 'undefined'
. I would have called it something else personally like 'not set'
but that's what JavaScript calls it, 'undefined'
. And if you don't in your code set it to anything else, that is what it will be. Or if you set it to something else later, and use it beforehand that is what it will be. Alright, so that's JavaScript and 'undefined'
."
JavaScript: Understanding the Weird Parts, Lecture 11: Conceptual Aside: JavaScript and 'undefined'