In JavaScript, null is a primitive value that represents the intentional absence of any object value. It is one of JavaScript's falsy values, meaning it evaluates to false in boolean contexts. However, it is distinct from undefined, which represents a variable that has been declared but has not been assigned a value.
null is used when a variable is expected to hold an object but currently doesn't have one. It signals the intentional absence of a value.
let person = null; // The `person` variable explicitly has no value.
Used when you intentionally want to signify "no value" or "empty."
In JavaScript, undefined is a primitive value automatically assigned to variables that have been declared but not yet assigned a value.
let x; console.log(typeof x); // "undefined"
In this example, uninitializedVariable is undefined because it hasn't been assigned any value.
Meanwhile, objectWithNoValue is explicitly set to null, signaling that it should hold an object, but currently it doesn’t.
console.log(null == undefined); // true
console.log(null == undefined); // true
The above is the detailed content of What is the difference between Null and undefined in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!