Representing "Nothing"
Representing the lack of a value is confusing in JavaScript since there are two different ways to do it: null and undefined.
What Are null and undefined?
Both null and undefined represent the lack of a value, but they're used in different situations:
undefined- means a variable exists but hasn't been given a value yetnull- means you've intentionally set a variable to have no value
Understanding undefined
When you create a variable but don't give it a value, JavaScript automatically sets it to undefined:
let a
console.log(a) // undefined
There are other cases where you might see undefined, which will be covered later, but it always means a value does not exist.
Understanding null
null is a value used to indicate "intentionally empty":
let a = null
console.log(a) // null
By setting a value to null, you are explicitly saying this variable has no value.
Best Practices
Use
nullfor Intentional "Empty" Stateslet selectedFile = null let currentTheme = null let lastError = nullLet
undefinedHappen Naturallylet userName // Will be set later let userPreferences // Will be loaded from serverUse
nullto Clear Valueslet name = "Kyle" // Other code uses name... name = null // Now name has no value
The Key Difference
undefined- I haven't been given a value yetnull- I specifically have no value