# null vs undefined

> What is the difference between null and undefined in JavaScript?

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-null-undefined
Last updated: 2026-11-28

Difficulty: Beginner

## Question

What is the difference between null and undefined in JavaScript?

## Answer

Both null and undefined represent "absence of value" but they serve different purposes:

- undefined: variable has been declared but not assigned a value. It is the default value assigned by JavaScript.
- null: an intentional assignment indicating "no value" or "empty". It is explicitly set by the programmer.

Key differences:

1. typeof undefined === "undefined", typeof null === "object" (historic bug)
2. undefined is the default for uninitialized variables, missing function args, missing object properties
3. null must be explicitly assigned
4. null == undefined is true (loose), null === undefined is false (strict)

**null vs undefined under == and ===. Switch to === or compare null with 0 under Tweak.**

1. **Both are nullish**: They mean "no value": undefined by default, null on purpose.
2. **null == undefined is true**: A special rule: they are loosely equal to each other and nothing else.
3. **null === undefined is false**: Different types.
4. **typeof differs**: typeof undefined is "undefined"; typeof null is "object".

## Code examples

### undefined Scenarios

```
// Declared but not assigned
let a;
console.log(a); // undefined

// Missing function parameter
function greet(name) {
  console.log(name);
}
greet(); // undefined

// Non-existent object property
const obj = { x: 1 };
console.log(obj.y); // undefined

// Function with no return
function doNothing() {}
console.log(doNothing()); // undefined

console.log(typeof undefined); // "undefined"
```

JavaScript assigns undefined automatically in these cases.

### null Usage

```
// Intentional empty value
let user = null; // user will be assigned later
console.log(user); // null
console.log(typeof null); // "object" (known JS bug)

// Clearing a reference
let data = { key: "value" };
data = null; // explicit cleanup

// Checking for both
function isEmpty(val) {
  return val == null; // catches both null and undefined
}
console.log(isEmpty(null));      // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty(0));         // false
console.log(isEmpty(""));        // false
```

null is used intentionally. The == null check is a common shorthand for checking both null and undefined.

## Key points

- typeof null === "object" is a known bug from the first JavaScript implementation
- Use val == null to check for both null and undefined at once
- undefined is a type itself, null is an object (technically)
- JSON.stringify ignores undefined properties but keeps null

## Common follow-up questions

- Why does typeof null return "object"?
- How does nullish coalescing (??) handle null vs undefined?
- What does void 0 return and why was it used?

## Interview tips

- Emphasize the semantic difference: undefined = unset, null = intentionally empty
- The typeof null === "object" bug is a classic JS trivia question
- Show the val == null shorthand pattern - it demonstrates practical knowledge

## Concepts covered

null, undefined, Type Checking, Default Values

Previous: [Template Literals & String Interpolation](https://www.internhack.in/learn/interview/javascript-interview/js-template-literals) · Next: [JSON: parse, stringify & Common Pitfalls](https://www.internhack.in/learn/interview/javascript-interview/js-json-parse-stringify) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Closures & Lexical Scope](https://www.internhack.in/learn/interview/javascript-interview/js-closures-interview)
- [Hoisting & Temporal Dead Zone](https://www.internhack.in/learn/interview/javascript-interview/js-hoisting-interview)
- [Object Methods & Property Descriptors](https://www.internhack.in/learn/interview/javascript-interview/js-object-methods)
- [this Keyword & Binding Rules](https://www.internhack.in/learn/interview/javascript-interview/js-this-keyword)
- [call, apply, bind](https://www.internhack.in/learn/interview/javascript-interview/js-call-apply-bind)
- [Prototypes & Prototype Chain](https://www.internhack.in/learn/interview/javascript-interview/js-prototypes)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
