1

How is null equal to undefined and not equal to undefined within the same operator.

undefined == null

true

undefined !== null

true

undefined === null

false

undefined != null

false

  • Hi, I think this answer your question, https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – Yash Maheshwari May 10 '21 at 15:01
  • *"How is null equal to undefined and not equal to undefined within the same operator."* It's not? But anyways, maybe this helps: https://felix-kling.de/js-loose-comparison/#null==undefined – Felix Kling May 10 '21 at 15:02
  • Simply, `==` only checks for value, whereas `===` checks for value as well as type of data. Here undefined and null both means 0 so when using `==` it's true, but when using `===` is false because undefined is type of undefined and null is type of object. – Yash Maheshwari May 10 '21 at 15:03
  • @YashMaheshwari: Small correction, `null` is of type Null. – Felix Kling May 10 '21 at 15:04
  • @FelixKling Thanks for correcting me, but when checking `typeof(null)` in the browser console, it returns Object. – Yash Maheshwari May 10 '21 at 15:05
  • 1
    @YashMaheshwari: Because `typeof` is .... not great. https://tc39.es/ecma262/#sec-ecmascript-language-types-null-type – Felix Kling May 10 '21 at 15:06
  • Thanks, for clearing the doubt. – Yash Maheshwari May 10 '21 at 15:07
  • This is explicitly mentioned in the [specification](//tc39.es/ecma262/#sec-abstract-equality-comparison). – Sebastian Simon May 10 '21 at 18:46
  • 1
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+why+is+undefined+%3D%3D+null) of [why null==undefined is true in javascript](/q/16607761/4642212). – Sebastian Simon May 10 '21 at 18:47

3 Answers3

0

Undefined and null are empty variables. But to understand whats above you have to understand comparison operators. == is a comparison operator that can convert. === really tests if its the same without doing type conversion. So null is practically the same as undefined. Its a bit hard to understand when you first think about it but its really not that hard. Some examples:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true
RANGO
  • 192
  • 9
  • 1
    They are empty "values", not "variables". `==` is a comparison operator, not a (the) conditional operator (that's `... ? ... : ...`). – Felix Kling May 10 '21 at 15:04
  • The variable has no value so its empty – RANGO May 10 '21 at 15:14
  • You are somehow mixing *variables* with *values*. A variable is a container for a value. null and undefined are values (`undefined` is a also a variable, but lets not get into that). The only time a variable would be truly *empty* is with `let foo;`. You can't access it until it was assigned a value. – Felix Kling May 10 '21 at 19:20
  • If you write `let foo; console.log(foo);` it returns undefined. A variable is a container that contains a value and if the value is empty the container doesn´t contain anything. Maybe english is more specific about that but at least in german you say a variable is empty – RANGO May 12 '21 at 14:09
  • I guess I was thinking of the `console.log(foo); let foo;`. Anyways, I have no problem when people say call a variable "empty" if it contains `null` or `undefined`. But that's not what you said. You said *"Undefined and null are empty variables."* There is a big difference between "A variable containing undefined or null is empty" and "undefined and null are empty variables". – Felix Kling May 12 '21 at 18:48
0

== converts the variable values to the same type before performing the actual comparison(making type coercion)

=== doesn't do type coercion and returns true only if both values and types are identical for the two variables being compared.

Let's take a look at some examples:

console.log(5 == "5"); // no type coercion is being made - hence, the result is true.

console.log(5 === "5"); // type coercion is being made, value is the same but the data types are not (number vs string);

console.log(undefined == null); // should be true because type coercion is not being made and the data values are both falsy!

console.log(undefined !== null); // should be true cause type coercion is being made and the data types are differnt!

console.log(undefined === null); // // should be false cause type coercion is being made and the data types are differnt.


console.log(undefined != null); // should be false cause type coercion is not being made and the data values are both falsy!
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • That's true but there is no coercion happening in this case. The algorithm simply states to treat `null` and `undefined` as equal. – Felix Kling May 10 '21 at 15:06
0

Please find my answer below.

"==" coerces the type of the variable before making a comparison. i. so undefined == null is true since both the variables coerces to false (since they both indicate an empty value) and post that comparison is made making them equal.

!== makes a strict comparison so the type is not changed, in case of undefined the type is "undefined" and in case of null the type is "object" which can be verified using typeof.

ii. so since the type does not match,!== return true i.e. undefined !== null.

iii. Same way === makes a strict comparison, so undefined === null is false, since the type is only different

iv. Lastly undefined != null is false, since != like == coerces the type of the variables to false, and then compares. Hence they both seem equal to != and it return false.

Suruchi
  • 76
  • 5