Skip to main content
InterviewHubPRO
Section Overview
Q79

Question 79 of 536

Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)

15%
JavaScript & V8 Internals (536 Qs)•Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)

What is the difference between null and undefined

junior LevelMost AskedCode ExampleAsync & Promises

Below are the main differences between null and undefined,

NullUndefined
It is an assignment value which indicates that variable points to no object.It is not an assignment value where a variable has been declared but has not yet been assigned a value.
Type of null is objectType of undefined is undefined
The null value is a primitive value that represents the null, empty, or non-existent reference.The undefined value is a primitive value used when a variable has not been assigned a value.
Indicates the absence of a value for a variableIndicates absence of variable itself
Converted to zero (0) while performing primitive operationsConverted to NaN while performing primitive operations

Practical Code Example

javascript
// undefined: Variable declared but not assigned a value
let a;
console.log(a);               // undefined
console.log(typeof undefined); // 'undefined'

// null: Intentional absence of any object value
let b = null;
console.log(b);               // null
console.log(typeof null);     // 'object' (famous JavaScript legacy bug)

// Comparison:
console.log(null == undefined);  // true (loose equality)
console.log(null === undefined); // false (strict equality)