Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a == null and a === null? [duplicate]

Tags:

javascript

Can someone help me by explaining the difference. From what I understand the === does an exact match but what does this mean when comparing with null ?

like image 647
Samantha J T Star Avatar asked Dec 19 '13 17:12

Samantha J T Star


People also ask

What is the difference between the == and === operators?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

Why would you use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

IS null === undefined?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

What does === means?

The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.


1 Answers

What does this mean when comparing with null?

It means exactly what you already said: It checks whether the value is exactly null.


a === null is true if the value of a is null.

See The Strict Equality Comparison Algorithm in the specification:

1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.

So, only if Type(a) is Null, the comparison returns true.

Important: Don't confuse the internal Type function with the typeof operator. typeof null would actually return the string "object", which is more confusing than helping.


a == null is true if the value of a is null or undefined.

See The Abstract Equality Comparison Algorithm in the specification:

2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.

like image 200
Felix Kling Avatar answered Oct 19 '22 19:10

Felix Kling