473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why is 0 == "" in JavaScript?

Why is
0 == "" in JavaScript

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

thank you.
Gap
Mar 21 '08 #1
7 1302
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 21 '08 #2
ga*****@gmail.com wrote:
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==""
Mar 21 '08 #3
ga*****@gmail.com wrote:
Why is
0 == "" in JavaScript

Since "A" + 0 != "A" + ""
Mistakes were made.
Mar 23 '08 #4
Thomas 'PointedEars' Lahn wrote:
ga*****@gmail.com wrote:
>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
i think i see your answer to the OP is correct,
but how do you explain the apparent contradiction
0 == ""
AND
0 == "0"

?

Mar 29 '08 #5
Rick Merrill wrote:
Thomas 'PointedEars' Lahn wrote:
>ga*****@gmail.com wrote:
>>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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Mar 29 '08 #6
Rick Merrill <ri***********@gmail.comwrites:
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.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Mar 29 '08 #7
Joost Diepenmaat wrote:
See section 11.8.5 of the emacascript 262 specs:
It is ECMA-262 or ECMAScript, not both. Otherwise you are correct.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Mar 29 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!--...
3
by: NecroJoe | last post by:
I am using PHP to generate a little javascript for one of my pages. In short it allows a user to select a value from a list and pop it into a form field on a seperate page. This works well unless...
9
by: David D. | last post by:
Does the file extension matter when including a JavaScript file in an HTML page? Normally, one would include a JavaScript file in an HTML page using <script src="foo.JS" type="text/javascript">...
6
by: Jon Davis | last post by:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using: <object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object> ...
0
by: Joeyej | last post by:
Hi - I'm trying to move/use a web form (containing some javascript field checks) previously hosted on a Windows 2000 server. However, the FORM METHOD="post..." command in the form (shown below)...
7
by: RFS666 | last post by:
Hello, I would like to use variables with a type in jscript.NET. I declare them as follows: var x : double = 5.03; This doesn't work in my script, that I write to the page in codebehind with...
6
by: kelvlam | last post by:
Hello, I'm a new begininer with JavaScript. I'm trying to figure out which is the best approach, and to understand the differences between them. I have a <Aelement that's suppose to either...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
2
by: Navodit | last post by:
Hi I am new to Javascript and am not sure what is the difference between the following two statements: <script type="text/javascript"> ..... </script>
5
by: dangt85 | last post by:
Hello, I have the following page: ... <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.