The first way it's used is to indicate that a declared variable (
var foo) has no assigned value. The second way it's used is to indicate that an object property you're trying to access is not defined (it has not even been named), and is not found in the prototype chain.
In the following sample, I examine both usages of
undefined. You should never find yourself setting a value to
undefined, as in
foo = undefined. Instead,
null should be used if you are specifying that a property or variable value is not available.
undefined declared in the global scope. Because the variable is declared and not assigned a value, the undefined variable is set to
<!DOCTYPE html><html lang="en"><body><script> // Confirm that undefined is a property of the global scope. console.log(undefined in this); // Logs true. </script></body></html>
Having a good understanding of the
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post