Why is 0 == "" in JavaScript?

E

Evertjan.

wrote on 21 mrt 2008 in comp.lang.javascript:
Why is
0 == "" in JavaScript

because

var result = 0 == "";

is not the same as

var result = 0 === "";

==========

With:

var result = 0 == "";

both being near equivalent to false,
and


var result = false == false;

results in true.
 
T

Thomas 'PointedEars' Lahn

Why is
0 == "" in JavaScript

Steps of evaluation (cf. ECMAScript Ed. 3 Final, sections 11.9.1 to 11.9.3):

0 == ""
+0 == +0
true
Since "A" + 0 != "A" + ""

"A" + 0 != "A" + ""
!("A" + +0 == "A" + "")
!("A" + "0" == "A" + "")
!("A0" == "A")
!(false)
true


PointedEars
 
S

Stevo

Why is
0 == "" in JavaScript

Since "A" + 0 != "A" + ""

Well, "A"+0 is "A0".

As for 0 == "", that's all done to automatic type conversions and the
fact that javascript considers false, 0, "", undefined all as false, and
the rest as true. So 0 is false and "" is false, therefore 0==""
 
L

Logos

Why is
0 == "" in JavaScript

Since "A" + 0 != "A" + ""

thank you.
Gap

A more verbose summing up of the others' posts:

0 == "" being true is a result of how javascript does automatic type
casting for you, depending on which equality operator you use.
Javascript is a loosely typed language, and as such does conversion
behinds the scenes for you that can produce unexpected results if you
don't know the conversion rules.

Loosely speaking:

== is loose (equality) - it allows javascript to do type conversion as
it sees fit. And as others pointed out, the specifications state that
the number zero and an empty string are considered equal.

=== is strict (identity) - it tells javascript that type must match as
well as content.

So 0 == "" results in true, but 0 === "" produces false.

Your "A"+0 produces "A0" because whenever there is a string by a plus
sign, javascript interprets the plus sign as a concatenation operator,
not as an addition operator. Since javascript is loosely typed, this
means it casts the zero to a string and then concatenates it to the
other string.

So javascript interprets "A"+0 as "cast 0 to a string and the
concatenate it with the string A", producing "A0". Which is not equal
to "A" concatenated with an empty string.

Hopefully that makes the more terse posts more understandable :)

Tyler
 
L

Lasse Reichstein Nielsen

Douglas Crockford said:
Mistakes were made.

Design choices were made. Some people disagree with those choices.

In Javascript, most operations cannot give a type error. If you try
an operation that isn't directly supported on the operand, the operand
is type converted to something that do work. And that often does what
you want, but not always.

This allowed a lot of people to use Javascript that didn't have the
abilities and rigor normally required for programming (like
remembering the type of ones values). The code wasn't pretty, but it
"worked" (kindof, sortof, almost), and without this, Javascript would
probably never have reached the penetration that caused it to become
the standard for web scripting.

You can dislike the choices, but without them, I doubt we would be
working with Javascript now.
/L
 
T

Thomas 'PointedEars' Lahn

Rick said:
Thomas said:
Why is
0 == "" in JavaScript
Steps of evaluation (cf. ECMAScript Ed. 3 Final, sections 11.9.1 to 11.9.3):

0 == ""
+0 == +0
true
Since "A" + 0 != "A" + ""
"A" + 0 != "A" + ""
!("A" + +0 == "A" + "")
!("A" + "0" == "A" + "")
!("A0" == "A")
!(false)
true
[...]

Please trim your quotes, do not quote signatures unless referring directly
to them.
i think i see your answer to the OP is correct,
but how do you explain the apparent contradiction
0 == ""
AND
0 == "0"

What is perceived as a contradiction by the OP and you here is ignoring that
string concatenation also involves type conversion.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Rick said:
[...] how do you explain the apparent contradiction
0 == ""
AND
0 == "0"

?

There is no contradiction. The steps of evaluation are:

0 == ""
+0 == ""
+0 == ToNumber(""), because Type(+0) is Number and Type("") is String
+0 == +0
true

and

0 == "0"
+0 == ToNumber("0"), because Type(+0) is Number and Type("0") is String
+0 == +0
true

You should read the sections of the Specification I have referred to already.


PointedEars
 
J

Joost Diepenmaat

Rick Merrill said:
i think i see your answer to the OP is correct,
but how do you explain the apparent contradiction
0 == ""
AND
0 == "0"

That's not a contradiction, that's just type conversion.

See section 11.8.5 of the emacascript 262 specs:

The Abstract Equality Comparison Algorithm

The comparison x == y, where x and y are values, produces true or
false. Such a comparison is performed as follows:

1. If Type(x) is different from Type(y), go to step 14.

[ snip ]

14. If x is null and y is undefined, return true.
15. If x is undefined and y is null, return true.
16. If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[ snip ]

ToNumber("0") and ToNumber("") both evaluate to 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top