Null vs Undefined in JavaScript: What’s the Difference?
17 August 2026
Understand the differences between null and undefined in JavaScript. Learn with practical examples, type checks, and best practices for UK developers in 2026.
Quick Answer: Null vs Undefined
In JavaScript, null and undefined both represent 'empty' values, but they have distinct meanings. undefined means a variable has been declared but has not been assigned a value. null is an intentional assignment that represents 'no value' or 'nothing'. For example, let a; logs undefined, while let b = null; stores null. Understanding this distinction is crucial for debugging and writing predictable code. In 2026, with modern JavaScript frameworks, proper handling of these values prevents common runtime errors. Developers in the UK, especially those working with Node.js or React, need to be precise about when to use each.
What is Undefined in JavaScript?
undefined is a primitive value automatically assigned to variables that have been declared but not initialised. It is also the default return value for functions that do not explicitly return anything. For instance, function test() {} returns undefined. Accessing an object property that does not exist also yields undefined. In JavaScript’s type system, undefined is a type itself. When you use typeof on an undeclared variable, it returns 'undefined' without throwing an error, which is unique and can be useful for feature detection. However, relying on undefined can lead to unexpected behaviour if you forget to initialise a variable.
What is Null in JavaScript?
null is a primitive value that represents the intentional absence of any object value. Unlike undefined, null must be explicitly assigned by the developer. For example, let user = null; indicates that the user object is intentionally empty for now. This is often used when a variable is expected to hold an object later. Interestingly, typeof null returns 'object' in JavaScript, which is a historical bug that persists in the language. In practice, null is useful for resetting values, clearing references, and signalling that a value is not available. UK developers often use null in APIs to indicate optional fields that are not provided.
Key Differences and Type Checking
The core difference is intention. Undefined means 'not defined' or 'missing', while null means 'defined as nothing'. When checking types, strict equality (===) distinguishes them: null === undefined is false, but null == undefined is true due to loose equality coercion. To check for null, use value === null. To check for undefined, use value === undefined or typeof value === 'undefined'. For a combined check, you can use value == null to catch both, but this is often frowned upon. In 2026, modern JavaScript offers Optional Chaining (?.) and Nullish Coalescing (??) to handle these safely, reducing bugs in production code.
Best Practices and Common Pitfalls
Adopt a clear convention: leave undefined to the JavaScript engine and use null for intentional empty values. This makes your code self-documenting. For example, a function parameter that is optional could default to null, not undefined. Avoid reassigning variables to undefined unless absolutely necessary. Use default parameters or destructuring with defaults to prevent undefined propagation. Common pitfalls include thinking typeof null is 'null' (it's 'object'), and assuming loose equality is safe — it can hide bugs. UK teams should enforce linting rules such as eqeqeq to ensure strict equality and always handle edge cases in code reviews.
FAQ
Yes, null == undefined is true because loose equality (==) compares after type coercion and treats them as equivalent. However, null === undefined is false because they are different types. For accurate checks, always use strict equality or explicit checks like value === null or typeof value === 'undefined'.