Interview Q&A / JavaScript

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the difference between null and undefined?

SHORT ANSWER

undefined means a value was never assigned: JavaScript gives it to you automatically. null means 'no value' and is set on purpose by the programmer. They are loosely equal (null == undefined) but not strictly equal.

Explain it simply

Think of a hostel room allotment form. If the "Room number" field is blank because nobody filled it yet, that is undefined. If the warden writes "No room" on purpose, that is null. Both mean "nothing", but one is accidental and one is deliberate.

JavaScript produces undefined on its own in many places:

  • a declared variable with no value
  • a missing object property
  • a function parameter that was not passed
  • a function with no return

null never appears unless some code assigns it.

Example

js
let seat;
const passenger = { name: "Ravi" };
function book() {}

console.log(seat === undefined);            // Output: true
console.log(passenger.age === undefined);   // Output: true
console.log(book() === undefined);          // Output: true

let coupon = null;                          // set on purpose
console.log(typeof coupon);                 // Output: object
console.log(typeof seat);                   // Output: undefined
console.log(null == undefined);             // Output: true
console.log(null === undefined);            // Output: false
console.log(null + 1);                      // Output: 1

null + 1 gives 1 because null converts to 0, while undefined + 1 gives NaN.

What to say in the interview

"undefined is the default 'not assigned' value that the engine gives you. null is an intentional empty value that I assign myself, for example to clear a selected item. They are loosely equal to each other and to nothing else, so value == null is a handy check for both. In numbers, null becomes 0 and undefined becomes NaN."

Common mistakes

  • Assigning undefined yourself to mean "empty". Prefer null so readers know it was intentional.
  • Using value == null without knowing it also matches undefined.

#javascript#types

Related questions

Practise more JavaScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open JavaScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.