Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript falsy values (null, undefined, false, empty string: "" or '' and 0) and comparison(==) operator [duplicate]

When I am using any one of values(null, undefined, false, '', 0) in a if statement, it is always evaluated as fallacy(false). Also, the negation of these values((null, undefined, false, '', 0) in a if statement always evaluated as tautology(true).

 if(null){
 }else{
 }

 if(undefined){
 }else{
 }

 if(false){
 }else{
 }

 if(''){
 }else{
 }

 if(0){
 }else{
 }

In all the above cases, if statement is evaluated as false & else statement executes. However, when I am comparing these fallacy values with == operator, it is not returning true always. Surprisingly, it is always returning true values when I am comparing the negation of these values.

if double equalto (==) operator checks/compares for values & not strictly for types, then why:

null == false          // returns false

null == 0              // returns false

null == ''             // returns false

But,

!null == !false       // returns true

!null == !0           // returns true

!false == !undefined  // returns true

And,

null == undefined     // returns true

false == 0            // returns true

I appreciate if any one can clarify the behavior or relationship among these values(null, undefined, false, '', 0).

like image 847
Suraj Kumar Avatar asked Jan 18 '14 15:01

Suraj Kumar


People also ask

What are the Falsy values in JavaScript?

There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.

Is empty string considered Falsy?

Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just use not string.

Is undefined and null are false in JavaScript?

Both undefined and null are falsy by default. So == returns true. But when we use the strict equality operator (===) which checks both type and value, since undefined and null are of different types (from the typeof Operator section), the strict equality operator returns false.

How do you check if a string is empty or undefined in JavaScript?

Say, if a string is empty var name = "" then console. log(! name) returns true . this function will return true if val is empty, null, undefined, false, the number 0 or NaN.


3 Answers

A common misconception

"...If double equalto (==) operator only checks/compares for values & not for types..."

That's an incorrect assumption, though it's often repeated by people. In reality, the == does check types, and in fact pays far more attention to the types than a === comparison does.

See Abstract Equality Comparison Algorithm.

A == comparison doesn't do a simple toBoolean conversion. Rather it walks through a somewhat complex recursive algorithm, which, after checking the types, attempts to coerce the operands to the same type if they don't match.

The type coercion that it performs is very specific to the types of the operands. A different sequence of coercions can take place for different type pairs. Usually (but not always) it ends up ultimately coercing the operands down to number types.


Why !ing the operands changes things

When you manually coerce both operands using !, you're now doing a simple toBoolean conversion causing the types to match, which avoids the type coercive part of the algorithm, making it behave essentially like the Strict Equality Comparison Algorithm.

So the only way to predict the outcome of a == comparison when the types don't match is to understand that Abstract algorithm.


Don't forget about NaN

And FYI, there's one more "falsey" value to consider, NaN. Its == comparison will always be false, no matter what. Even when comparing to another NaN value, it'll be false.

like image 95
cookie monster Avatar answered Oct 19 '22 02:10

cookie monster


undefined: means a variable was declared but has no value assigned

null: the value of null has been assigned, which means it has no value

false, '' and 0 I think you can probably work out what these mean.

like image 33
Todd Motto Avatar answered Oct 19 '22 03:10

Todd Motto


NULL is different from false (NULL is of type object and false is of type of boolean), null is different from 0 (0 is of type integer), null is also different from '' ('' is of type of string). but they are all falsy values. The ! operator negates a boolean value. If ! is used on falsy values, it results to conversion of falsy values to an object of type boolean.

like image 25
Allan Chua Avatar answered Oct 19 '22 03:10

Allan Chua