最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does null == undefined evaluate to true? - Stack Overflow

programmeradmin1浏览0评论

I have a code below which is not clear to me

var a = null; 
if(a==undefined)
alert("true");
else 
alert("false");

When I ran above code it alerts true.

Can anybody explain whats the reason or concept behind this?

I have a code below which is not clear to me

var a = null; 
if(a==undefined)
alert("true");
else 
alert("false");

When I ran above code it alerts true.

Can anybody explain whats the reason or concept behind this?

Share Improve this question edited Jul 28, 2016 at 8:08 T.J. Crowder 1.1m199 gold badges2k silver badges1.9k bronze badges asked Jul 28, 2016 at 8:01 Vijay BarnwalVijay Barnwal 1541 silver badge5 bronze badges 7
  • 3 Try === instead of == ;-) – Álvaro González Commented Jul 28, 2016 at 8:02
  • datatype of null is "object" whereas for undefined its "undefined" and I think there is no similarity for null and undefined then why its print true? – Vijay Barnwal Commented Jul 28, 2016 at 8:03
  • Note the details for the abstract equality operator (or rather just a copy of the specs petty much) is also defined in the SO Documentation. – Spencer Wieczorek Commented Jul 28, 2016 at 8:11
  • Hi Alvaro yes I know === compares value as well as datatype but in my case I only check value between null and undefined how is it possible that null and undefined both having same value? Is there any concept or my confusion to understand this? – Vijay Barnwal Commented Jul 28, 2016 at 9:13
  • @VijayBarnwal: They don't have the same value, but their values are loosely equal according to the definition of loose equality in JavaScript. – T.J. Crowder Commented Jul 28, 2016 at 9:18
 |  Show 2 more comments

7 Answers 7

Reset to default 16

It's true because == is the loose equality operator, and null and undefined are loosely equal (null == undefined is true). If you use the strict equality operator, ===, they are not equal (null === undefined is false).

Basically, the loose equality operator will coerce its operands if they're of different types (see Abtract Equality Comparison in the spec). 0 == "" is true, for instance, because if you coerce "" to a number, it's 0. The strict equality operator considers operands of different types not equal (see Strict Equality Comparison); it does not coerce.


On browsers, there's a third value that's == to null and undefined: document.all. document.all behaves like undefined in various specification operations. This is so that really, really old code that was using if (document.all) or similar to go an IE-specific direction doesn't do that on modern browsers, since the features that they were avoiding exit on IE versions that handle document.all this way. This is defined in the spec.

The specifications for the == operator is defined so that null == undefined returns true.

The spec - Clause 11.9.3 . See clauses 2 and 3.

Comparison operators operate by converting operands into numbers before comparing. I understood this by an example, which is: Consider you're writing a program to give access to employees into their account. You ask for credentials and the user input nothing. Ideally, you keep asking until it's provided because null is equal only to undefined under ==, which a special case in comparison operation.

If a variable is pointing to nothing, it's null. That's similar in concept to an undefined variable, which has not yet been initialized.

Javascript interpreted this to make loose equality (==) treat null and undefined the same way. Strict equality (===) is more picky; if you have different data types such as null and undefined, they won't be equal.

See What is the difference between null and undefined in JavaScript? on differences between undefined and null and Which equals operator (== vs ===) should be used in JavaScript comparisons?

undefined means a variable has not been defined, or given a value. null is a special object within javascript.

Comparing the values using equals == will always give you true because they are both basically the same in value (0, nothing, nada, zip).

Using strict equals === though, will return false because null is an object and undefined is just a blank variable.

This behavior stems from JavaScript being a loosely-typed language. In the case of equality, this means values of differing data types can be compared.

Both null and undefined are considered "falsy", therefore they're loosely considered equal.

Logically, if null == false and undefined == false, then null == undefined.

发布评论

评论列表(0)

  1. 暂无评论