0

How is this possible that null and undefined is not equal?

Please anyone help me to this issue Why that null and undefined is not equal?

28th Apr 2023, 6:00 AM
Farhad Rahmani
Farhad Rahmani - avatar
4 Antworten
+ 5
null and undefined are two distinct types with distinct values, and therefore not equivalent. Specifically, null is a value that signifies the intentional absence of any object value. It is assigned to a variable to indicate that it is intentionally empty, and is recognized as a special keyword in JavaScript treated as a primitive value. Conversely, undefined is a primitive value assigned to a variable when it has not been assigned any value or when a function fails to return a value. Although both null and undefined signify the absence of a value, they bear distinct connotations and thus are not considered equal to each other.
28th Apr 2023, 11:46 AM
Sadaam Linux
Sadaam Linux - avatar
+ 4
For example: var x = null; var y = undefined; console.log(x == y); // true console.log(x === y); // false the first comparison x == y returns true because the double equals operator == performs type coercion, which converts null to undefined, making both variables equal. However, the second comparison x === y returns false because the triple equals operator === does not perform type coercion, and the variables have different types and values.
28th Apr 2023, 11:46 AM
Sadaam Linux
Sadaam Linux - avatar
+ 2
But they *are* equal,, insofar as (null == undefined) evaluates to true. Null === undefined is false because for some reason null is an object in JS, whereas undefined is of undefined type.
28th Apr 2023, 6:38 AM
Orin Cook
Orin Cook - avatar
+ 2
Remember that === checks also the datatype. Null is an object and underfined is not an object. So the expression is false.
28th Apr 2023, 6:57 AM
Ugulberto Sánchez
Ugulberto Sánchez - avatar