← Back to JavaScript, page 1

javascript-0013 · Easy fresher

What is the difference between null and undefined in JavaScript?

undefined means a value was never assigned: an uninitialized variable, a missing property, or a function with no return. null is a value that a programmer sets on purpose to say there is nothing here. They are loosely equal, but have different types and behave differently in JSON, defaults, and arithmetic.

Full answer

undefined is what JavaScript gives you when nothing has been supplied. A declared variable with no value, a missing object property, a parameter that was not passed, and the result of a function with no return are all undefined. Its type is undefined.

null is a value you write yourself to mean "empty on purpose", for example a user with no middle name. typeof null returns "object". That is a historical bug in the language that was never fixed, so do not use typeof to detect null.

They compare in a particular way. null == undefined is true, while null === undefined is false. In arithmetic null becomes 0 and undefined becomes NaN.

They also behave differently elsewhere. Default parameters and destructuring defaults apply only to undefined, not to null. JSON.stringify drops properties whose value is undefined but keeps null. The nullish coalescing operator ?? and optional chaining ?. treat both as "missing".

A common style is to use undefined for "not provided" and null for "deliberately empty", and to keep that choice consistent in an API.

javascript
let notAssigned;
const person = { name: 'Ria', middleName: null };

console.log(notAssigned, person.age, person.middleName);
console.log(typeof undefined, typeof null);
console.log(null == undefined, null === undefined);
console.log(null + 1, undefined + 1);

function welcome(name = 'guest') {
  return `Welcome, ${name}`;
}
console.log(welcome(undefined));
console.log(welcome(null));

console.log(JSON.stringify({ a: undefined, b: null }));

console.log(person.middleName ?? 'no middle name');
console.log(person.address?.city ?? 'no city');
console.log(Object.is(person.middleName, null));

Run with Node.js v24.22.1 as js-13.js; two runs gave identical output. Output: undefined undefined null undefined object true false 1 NaN Welcome, guest Welcome, null {"b":null} no middle name no city true

Key points

  • undefined means unassigned or missing; null means intentionally empty.
  • typeof null is "object" because of an old bug; typeof undefined is "undefined".
  • null == undefined is true; null === undefined is false.
  • Default values apply to undefined, not to null.
  • JSON.stringify keeps null and drops undefined properties.

Common mistakes

  • Checking for null with typeof, which returns "object".
  • Expecting a default parameter to replace null.
  • Using || for defaults and replacing valid values such as 0 or an empty string; ?? only replaces null and undefined.
  • Assigning undefined to a property to remove it instead of using delete when the key itself should vanish.

Follow-ups

  • Why does typeof null return "object"?
  • What is the difference between a || b and a ?? b?
  • What is the difference between delete obj.key and obj.key = undefined?

Tips

  • Give one sentence for each, then compare them in equality, JSON, and defaults.
  • Use the typeof null fact to show that you know the language history.
  • Say which one your team uses for "empty by choice" so the answer sounds practical.