Differences between null and undefined

5 阅读1分钟

Interviewer: Hi there! Today I'd like to discuss some fundamental concepts in JavaScript. Could you tell me about the differences between null and undefined?

Candidate: Sure! Both null and undefined are primitive data types in JavaScript, but they have several key differences. Undefined means a variable has been declared but not yet assigned a value, while null represents an "empty object pointer" and is typically used to initialize variables that are expected to later hold an object.

Interviewer: Can you give me an example of when undefined would occur?

Candidate: Of course. When we declare a variable without assigning a value, it defaults to undefined:

let x;
console.log(x); // undefined

Also, when a function doesn't return anything, it implicitly returns undefined.

Interviewer: Good. In what situations would you typically use null?

Candidate: Null is typically used when we want to explicitly indicate "no value" or "empty value". For example, when initializing a variable that will eventually point to an object, but doesn't have one yet:

let user = null; // user data not loaded yet

Interviewer: Interestingly, what do these two types return when using the typeof operator?

Candidate: That's a great question. typeof undefined returns "undefined", which makes sense. But interestingly, typeof null returns "object", which is actually a historical bug in JavaScript that was never fixed for compatibility reasons.

Interviewer: Is there anything to be aware of when comparing these two values?

Candidate: Yes, when comparing null and undefined using the double equals operator (==), it returns true because double equals performs type conversion:

console.log(null == undefined); // true

But when using triple equals (===), which compares both value and type, it returns false:

console.log(null === undefined); // false

Interviewer: You mentioned undefined is not a reserved word. What implications does this have?

Candidate: Since undefined is not a reserved word, theoretically it can be reassigned, which might cause unexpected behavior in code:

let undefined = 'new value';

This would throw an error in strict mode, but is allowed in non-strict mode. To safely get the undefined value, we can use void 0, which always returns undefined:

let safeUndefined = void 0;

Interviewer: Very comprehensive answer! One last question: can you briefly explain why we need these two different "empty value" concepts?

Candidate: That's a great question. The reason for having both is mainly to express different semantics: undefined represents a "not yet assigned" state, it's a system-level, default empty value; while null represents an "intentionally missing value", actively set by the programmer. This distinction makes code intentions clearer and helps us understand why a variable is empty.

Interviewer: Excellent explanation, thank you for your answer!