Null and Undefined
👨💼 Great job! You've learned about JavaScript's two "empty" values.
🦉 Key takeaways:
undefinedis JavaScript's default for "no value assigned"nullis what you use when you want to explicitly say "no value"typeof undefinedis"undefined"typeof nullis"object"(a famous JavaScript quirk!)null == undefinedistrue(loose equality)null === undefinedisfalse(strict equality)
Always use
=== (strict equality) in your comparisons! Loose equality (==)
has surprising behaviors that can cause bugs.In practice, you'll see
undefined more often - it's what you get when
accessing a property that doesn't exist or a function parameter that wasn't
passed. You'll use null when you want to explicitly clear a value or indicate
"nothing here on purpose."