JavaScript
Programming Concepts
Null Value
Undefined Value
Object-oriented Programming

Why is null an object and what's the difference between null and undefined?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In many programming languages, including widely-used ones like JavaScript, null often represents the intentional absence of any object value. It is an assigned value denoting that a variable points to no object or that there is no data. Even though null implies 'no value', it paradoxically itself is a value in Javascript, specifically assigned to signify 'empty' or 'nothing'. However, null is not an object by itself, but a primitive value.

Understanding null as an Object

In JavaScript, there's a commonly misunderstood aspect where typeof null returns "object", which might suggest that null is an object. Historically, this is considered a bug in the original JavaScript implementation. null, in reality, is a primitive type. It's supposed to be a type of its own (just like undefined) but due to a bug, it’s considered as an object. Here's an example:

javascript
let item = null;
console.log(typeof item);  // Output: "object"

Despite what typeof suggests, null is not actually an object but a unique primitive type.

Differences Between null and undefined

null and undefined in JavaScript both denote the absence of value but in slightly different ways:

  • null is an explicit value assigned to a variable. It represents "nothing", "empty", or "value unknown".
  • undefined means a variable has been declared but has not yet been assigned a value.

They are used in different scenarios based on what you are trying to convey in your code.

Practical Example:

javascript
1let a = null;       // a is explicitly nothing
2let b;              // b is undefined
3
4console.log(a == b);  // Output: true because null and undefined are loose equals
5console.log(a === b); // Output: false because they are of different types

Table: null vs undefined

Featurenullundefined
TypePrimitivePrimitive
typeof output"object" (due to a bug)"undefined"
Default ValueExplicit assignment neededAutomatically assigned by JavaScript
EqualityEqual to undefined (==) but not strictly (===)Equal to null (==) but not strictly (===)

When to Use null vs undefined

  • Use null:
    • When you need to intentionally denote the absence of a value in your code for variables.
    • When you need to reset or clear a variable for re-use.
  • Use undefined:
    • When checking if a variable has not been initialized.
    • When a function does not explicitly return a value; it returns undefined by default.

Dealing with null and undefined

Essential checks surrounding null and undefined are crucial because both represent the absence of a value but are treated differently under strict comparison. Thus, understanding when and how to use them is crucial in avoiding bugs especially in type-checking instances.

Conclusion

Understanding the subtle differences between null and undefined is crucial for managing absence of data efficiently in JavaScript. Remember that while they are often used interchangeably in loose equality checks, they signify different states of a variable: null is an intentional absence and undefined is typically due to lack of initialization.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions