Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why null==undefined is true in javascript

Tags:

If we alert(null==undefined) it outputs to true.

What is the logical reason for this.

Is this something that is hard coded in javascript or is there an explanation for this.

like image 944
Nav Avatar asked May 17 '13 11:05

Nav


1 Answers

The language specification explicitly says:

If x is null and y is undefined, return true

I'm not aware of any records of the language design process that explain the reasoning for that decision, but == has rules for handling different types, and "null" and "undefined" are both things that mean "nothing", so having them be equal makes intuitive sense.

(If you don't want type fiddling, use === instead).

like image 63
Quentin Avatar answered Sep 27 '22 19:09

Quentin