Why javascript treats 0 equal to empty string?

Related searches

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.

if statement - Why javascript treats 0 equal to empty string?, And the function returned true! Incorrectly thinking it was a new job. In other words: JavaScript thinks that the number zero is equal to empty string. As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date. EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type.


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
}

JavaScript thinks 0 equals empty string | by David Ford, In Javascript, the expression value in null undefined NaN empty string ("") 0 false The set of "truthy" and "falsey" values in JavaScript comes from the ToBoolean abstract Boolean | The result equals the input argument (no conversion). As such undefined is returned and compared against an empty string. When you perform this logic within a the parenthesis, the '' and the undefined don't know about the impending equality check that is waiting to happen - they just want to know which of them is going to survive this evaluation.


== 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.

Falsy values vs null, undefined, or empty string, Equals: a == b , please note the double equality sign == means the equality An empty string, just like false , becomes a zero. Treat any comparison with undefined/null except the strict equality === with exceptional care. By definition null, undefined, 0, "" is converted to false, (mostly) anything else is converted to true (but I don't want to be so precise). It does not mean the same, because the fields.length === 0 line. Empty arrays and other objects are converted to true. note: Javascript is sometimes a big mess


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.

Comparisons, Get code examples like "empty string in javascript" instantly right from your google search results //check if string is empty, null, or 0 isEmpty() � javascript treat null as empty string � js check if not empty � check for empty Code is valid JSON equivalent of the key/value pair shown that also preserves the� Okay, so why does false == 0 in JavaScript? It’s complex, but it’s because in JavaScript 0 is a falsy value. Type coercion will actually convert our zero into a false boolean, then false is equal to false. There are only six falsy values in JavaScript you should be aware of: false — boolean false; 0 — number zero “” — empty string


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 ==

empty string in javascript Code Example, The three most widely used type conversions are to string, to number, and to boolean. String Conversion – Occurs when we output something. Can be performed with String(value). The conversion to string is usually obvious for primitive values. Numeric Conversion – Occurs in math operations. Can be performed with Number(value).


Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques.


Topic: JavaScript / jQuery Prev|Next. Answer: Use the === Operator. You can use the strict equality operator (===) to check whether a string is empty or not. The comparsion str === "" will only return true if the data type of the value is string and it is not empty, otherwise return false as demonstrated in the following example: