Understanding the Differences Between null and undefined in JavaScript
This article explains the conceptual and practical differences between JavaScript's null and undefined values, covering their definitions, type checking methods, comparison behavior, arithmetic conversion, JSON serialization, and best practices for usage.
Preface
This article was written over two days of research, aiming for accuracy. If you have questions or alternative views, feel free to comment. Front‑end beginners can stop after the "Presentation" section; the rest is for deeper study.
Simple Distinction
In JavaScript both null and undefined represent emptiness, but undefined indicates a variable that has not been initialized, while null indicates an intentionally missing object reference.
undefined The variable is not defined at all. Hidden empty value.
null The variable is defined but points to no object in memory. Declared empty value.
MDN Definition
null
The literal null is not a property of the global object. It signals the absence of an object reference and is often used as a return value when an API expects an object but has none.
undefined
undefined is a property of the global object, i.e., a global‑scope variable. Its initial value is the primitive undefined .
A Classic Diagram
The following classic diagram (originally from a StackOverflow answer) illustrates the relationship between the two values:
Presentation
Before diving deeper into the differences, understand how null and undefined behave in JavaScript.
typeof
typeof null // 'object'
typeof undefined // 'undefined'Object.prototype.toString.call
Object.prototype.toString.call(null) // '[object Null]'
Object.prototype.toString.call(undefined) // '[object Undefined]'== vs ===
null == undefined // true
null === undefined // false
!!null === !!undefined // trueObject.getPrototypeOf(Object.prototype) // null
+ Operation and Number()
let a = undefined + 1 // NaN
let b = null + 1 // 1
Number(undefined) // NaN
Number(null) // 0JSON
JSON.stringify({a: undefined}) // '{}'
JSON.stringify({b: null}) // '{b:null}'
JSON.stringify({a: undefined, b: null}) // '{b:null}'Shadowing undefined
let undefiend = 'test'
function test(n) {
let undefined = 'test'
return n === undefined
}
test() // false
test(undefined) // false
test('test') // true
let undefined = 'test' // Uncaught SyntaxError: Identifier 'undefined' has already been declaredDeep Dive
Why does typeof null return "object"?
The result is a historic bug: early JavaScript stored values in 32‑bit slots, using the first three bits for the type tag. Objects use the tag 000 , and null was given that tag, so typeof null incorrectly reports "object".
Why does Object.prototype.toString.call(null) output "[object Null]"?
The toString method on Object returns the internal [[Class]] property formatted as [object Xxx] , where Xxx is the actual type, thus yielding [object Null] for null .
Why can't xxx.toString() return the variable type?
Because many built‑in prototypes override toString . To obtain the raw type you must call the original method via Object.prototype.toString.call(value) .
Why do == and === give different results?
According to ECMA‑262 §11.9.3, null == undefined evaluates to true , while strict equality === requires both type and value to match, so null === undefined is false . The article will explore this further in a future post.
Why do null + 1 and undefined + 1 behave differently?
During addition JavaScript performs implicit type conversion to Number . null converts to 0 , while undefined converts to NaN , leading to the observed results.
null → 0 when converted to a number.
undefined → NaN when converted to a number.
Why does JSON.stringify drop properties with undefined values?
According to the JSON specification, properties whose values are undefined are omitted during serialization. This is why {a: undefined} becomes {} after stringification.
let obj1 = { a: undefined }
let obj2 = {}
console.log(obj1.a) // undefined
console.log(obj2.a) // undefinedWhy can let undefiend = 'test' shadow the global undefined ?
JavaScript creates a read‑only global undefined , but it does not prevent a local variable named undefined . Modern JavaScript engines forbid this in strict mode, but older code can still shadow it, which is dangerous.
Summary
When to use undefined vs null
Both values are valid for representing emptiness, but the author prefers null for an explicit, intentional empty reference. If you need to use undefined , avoid declaring variables with let a; or let a = undefined; . Instead, use the explicit void 0 form:
let a = void 0;
ByteFE
Cutting‑edge tech, article sharing, and practical insights from the ByteDance frontend team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.