Junior
What is the difference between null and undefined?
sobes.tech AI
Answer from AI
undefined indicates the absence of an assigned value. The variable is declared, but no value has been assigned to it. It is the result of accessing a non-existent property of an object or an array element.
null is an explicit assignment of the "absence of a value". The developer intentionally indicates that the variable or object property has no value. It is an object, but not a primitive type (although typeof null returns 'object', which is a language history bug).
// undefined
let x;
console.log(x); // undefined
const obj = {};
console.log(obj.prop); // undefined
// null
let y = null;
console.log(y); // null
const obj2 = { prop: null };
console.log(obj2.prop); // null