Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why javascript treats 0 equal to empty string? [duplicate]

Possible Duplicate:
Implied string comparison, 0='', but 1='1'

Executing the following case in javascript, I get 0 equal to '' (an empty string)

var a   =   0; var b   =   '';//empty string if(a==b){     console.log('equal');//this is printed in console }else{     console.log('not equal'); } 

Could anyone guide that what is the reason behind this?

like image 380
netemp Avatar asked Sep 14 '12 09:09

netemp


People also ask

Is empty string and 0 in JavaScript?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters.

Does an empty string equal 0?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Why empty string is false JavaScript?

Truthy or Falsy When javascript is expecting a boolean and it's given something else, it decides whether the something else is “truthy” or “falsy”. An empty string ( '' ), the number 0 , null , NaN , a boolean false , and undefined variables are all “falsy”. Everything else is “truthy”.

Is 0 considered false JavaScript?

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.


2 Answers

Quoting the doc (MDN):

Equal (==)

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.

As a operand type here is Number, b gets converted to Number as well. And Number('') evaluates to 0.

This can be quite surprising sometimes. Consider this, for example:

console.log(0 == '0');  // true console.log(0 == '');   // true console.log('' == '0'); // O'RLY? 

... or this:

console.log(false == undefined); // false console.log(false == null);      // false console.log(null == undefined);  // fal.... NO WAIT! 

...and that's exactly why it's almost always recommended to use === (strict equality) operator instead.

like image 168
raina77ow Avatar answered Sep 24 '22 23:09

raina77ow


0, "" (Empty String), false all technically have the same value, when typecasted. If you need to strictly treat them, you can use ===.

It is the same with similar programming languages, like PHP.

var a = 0; var b = ''; //empty string if(a == b){     console.log('equal'); //this is printed in console }else{     console.log('not equal'); } 

To make a strict comparison:

if(a === b){     console.log('equal'); }else{     console.log('not equal'); //this is printed in console } 
like image 28
Praveen Kumar Purushothaman Avatar answered Sep 22 '22 23:09

Praveen Kumar Purushothaman