31

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?

Community
  • 1
  • 1
netemp
  • 4,115
  • 12
  • 43
  • 63

9 Answers9

51

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.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • Thanks for the details shared. type comparison definitely should be the preferred choice. But in our case, we had a challenge that this comparison was taking place in a function receiving values as arguments and user could pass 1 as '1' too which is permissible in javscript. In such a case, if we use type comparison then '1' would not be equal to 1. Due to this, we had to avoid type comparison. Don't know what can be the safest method for this? – netemp Sep 14 '12 at 10:16
  • 1
    @netemp I'd suggest using `parseInt(arg, 10)` construct. When `arg` is a string, it will be _parsed_ into number (and it's more consistent then `Number(arg)`), and when `arg` is a number, it'll get back to you unharmed. ) – raina77ow Sep 14 '12 at 10:20
  • Thankyou. Wasn't aware the type conversion was achieved using Number (which unlike parseInt does not return NaN in the case of '' or ' '). Answer also helped explain this for me, why: '' == 0 && ' ' == 0 // true. Yet: !!' ' == !!0 // false. With the latter the type conversion is done to each argument first (rather than the arguments either side of ==) therefore ' ' evaluates to truthy. – Moika Turns Mar 03 '17 at 14:02
5

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
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
2

== operator in javascript don't compare types so 0=='' === true (because as a number string '' evaluates to 0) or 0==false === true (because bool false evaluates to 0) to compare types to you can use === operator.

Here you'll find useful information about comparison.

Leri
  • 12,367
  • 7
  • 43
  • 60
1

== does typecasting. Always use ===.

In your example, the empty string of b is being converted to 0. So both a and b are the same.

Geuis
  • 41,122
  • 56
  • 157
  • 219
1

Because empty string represented as number is zero. If you compare apples and oranges you should think of how your particular orange would look if it were an apple.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

Because == checks value equality so false, 0 and empty strings are equals. Use identity check ===.

kbec
  • 3,415
  • 3
  • 27
  • 42
0

Javascript automatically converts variables of different types for comparison. That's a common feature in non-strictly typed languages.

If you need to compare variables and check the type, use the === operator.

chiborg
  • 26,978
  • 14
  • 97
  • 115
0

In most programming language(including javascript) ""(string), 0(integer), \x0(null) losely mean the same thing: "empty". What's happening is your javascript engine finds "" == 0 false, due to the == it converts 0 to an integer. Again this is false, so it converts 0 to null which is false, so then it converts 0 to an empty string. (Not sure if this is the correct order of conversion). To make the condition "exact" match(no conversion) use === inplace of ==

SReject
  • 3,774
  • 1
  • 25
  • 41
0

Because of coercion. It is usually a better idea to use === for comparisons in JavaScript.

skunkfrukt
  • 1,550
  • 1
  • 13
  • 22