Null and Undefined
๐จโ๐ผ JavaScript has two special values that represent "nothing" or "no value":
- undefined - A variable that hasn't been assigned a value yet
- null - An intentional "empty" or "nothing" value
These might seem similar, but they have different meanings:
undefinedmeans "this doesn't have a value yet"nullmeans "this intentionally has no value"
- Create a variable
notAssignedwithout giving it a value - Create a variable
intentionallyEmptyand set it tonull - Log both variables with their types using
typeof - Compare them with
==and===to see the difference
๐ฐ You can declare a variable without a value:
let myVariable
console.log(myVariable) // undefined
๐ฆ Fun fact:
typeof null returns "object" - this is actually a bug in
JavaScript from the very first version, but it can't be fixed now because too
much code depends on it!๐ null on MDN
๐ undefined on MDN