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.
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:
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:
nullis an explicit value assigned to a variable. It represents "nothing", "empty", or "value unknown".undefinedmeans 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:
Table: null vs undefined
| Feature | null | undefined |
| Type | Primitive | Primitive |
typeof output | "object" (due to a bug) | "undefined" |
| Default Value | Explicit assignment needed | Automatically assigned by JavaScript |
| Equality | Equal 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
undefinedby 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
- Why is setState in reactjs Async instead of Sync?
- Why is setState in reactjs Async instead of Sync?
- Why is setTimeout(fn, 0) sometimes useful?
- Why is the Date constructor deprecated, and what do I use instead?
- Why is the setTimeout with 0 seconds last to finish
- Why is there no Html.Button in IntelliSense in ASP.NET MVC 3?
- Why is this async function not behaving asynchronously?
- Why node.js async module stops after the first step using async.eachLimitarray, limit, function, callback?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.