473,626 Members | 3,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

0 == false == ""

Greetings,

Now I can understand that 0 equal false, but should "" also equal
false?

Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false . I assume this goes back to the original spec for
JavaScript.

Not very intuitive but I guess I can code around this.

- JsD
Jan 16 '08 #1
29 2411
Java script Dude wrote:
Now I can understand that 0 equal false, but should "" also equal
false?
Yes, it should.
Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .
Works as designed.
I assume this goes back to the original spec for JavaScript.
Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:

| 11.9.1 The Equals Operator ( == )
|
| The production EqualityExpress ion : EqualityExpress ion ==
| RelationalExpre ssion is evaluated as follows:
|
| 1. Evaluate EqualityExpress ion.
| 2. Call GetValue(Result (1)).
| 3. Evaluate RelationalExpre ssion.
| 4. Call GetValue(Result (3)).
| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)
| 6. Return Result(5).
|
| [...]
|
| 11.9.3 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.

| 5.2 Algorithm Conventions
|
| [...] Type(x) is used as shorthand for “the type of x”.

| [...]
| 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).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.
| 18. If Type(x) is Boolean, return the result of the comparison
| ToNumber(x) == y.
| 19. If Type(y) is Boolean, return the result of the comparison
| x == ToNumber(y).

| 9.3 ToNumber
|
| The operator ToNumber converts its argument to a value of type Number
| according to the following table:
|
| Input Type Result
| --------------------------
| Undefined NaN
| Null +0
| Boolean The result is 1 if the argument is true.
| The result is +0 if the argument is false.
| Number The result equals the input argument (no conversion).
| String See grammar and note below.
| Object Apply the following steps:
| 1. Call ToPrimitive(inp ut argument, hint Number).
| 2. Call ToNumber(Result (1)).
| 3. Return Result(2).

Therefore ("" == +0):

| 1. If Type(x) is different from Type(y), go to step 14.
| [...]
| 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).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.

| 9.3.1 ToNumber Applied to the String Type
|
| [...]
| A StringNumericLi teral that is empty or contains only white space is
| converted to +0.

Therefore (+0 == +0):

| 1. If Type(x) is different from Type(y), go to step 14.
| 2. If Type(x) is Undefined, return true.
| 3. If Type(x) is Null, return true.
| 4. If Type(x) is not Number, go to step 11.
| 5. If x is NaN, return false.
| 6. If y is NaN, return false.
| 7. If x is the same number value as y, return true.

| 5.2 Algorithm Conventions
|
| When an algorithm is to produce a value as a result, the directive
| “return x” is used to indicate that the result of the algorithm is
| the value of x and that the algorithm should terminate.
Not very intuitive
Yes, it is. For example, it allows for

if (stringValue)

without having to compare against the length of the string or to check for a
whitespace-only string value.
but I guess I can code around this.
Yes, you can. Use the Strict Equality Operator (`===') instead, which does
not perform implicit type conversion; it is well-supported:

http://PointedEars.de/es-matrix
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Jan 16 '08 #2
Thomas 'PointedEars' Lahn said the following on 1/15/2008 11:13 PM:
Java script Dude wrote:
>Now I can understand that 0 equal false, but should "" also equal
false?

Yes, it should.
>Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .

Works as designed.
>I assume this goes back to the original spec for JavaScript.

Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:
No, what is more important than what some Specification says is what
browsers actually do with code.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 16 '08 #3
Randy Webb <Hi************ @aol.comwrites:
Thomas 'PointedEars' Lahn said the following on 1/15/2008 11:13 PM:
>Java script Dude wrote:
>>Now I can understand that 0 equal false, but should "" also equal
false?

Yes, it should.
>>Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .

Works as designed.
>>I assume this goes back to the original spec for JavaScript.

Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:

No, what is more important than what some Specification says is what
browsers actually do with code.
And, for what it's worth (just for the OP):

aundro@paddy:~$ js
js# 0 == ""
true
js# 0 === ""
false
js#

I personally tend to use the === operator as often as possible.

Best,
Arnaud

Jan 16 '08 #4
In article <1c************ *************** *******@q39g200 0hsf.googlegrou ps.com>, Java script Dude <de********@yah oo.cawrote:
>Now I can understand that 0 equal false, but should "" also equal
false?
Yes.
>
Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false . I assume this goes back to the original spec for
JavaScript.

Not very intuitive but I guess I can code around this.
That's a NAPWAD: Not A Problem - Works As Designed.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
Jan 16 '08 #5
On Jan 16, 4:19*am,
a...@remove.thi s.and.keep.what .follows.ionics oft.com (Arnaud Diederen
(aundro)) wrote:
Randy Webb <HikksNotAtH... @aol.comwrites:
Thomas 'PointedEars' Lahn said the following on 1/15/2008 11:13 PM:
Java script Dude wrote:
Now I can understand that 0 equal false, but should "" also equal
false?
Yes, it should.
>Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .
Works as designed.
>I assume this goes back to the original spec for JavaScript.
Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:
No, what is more important than what some Specification says is what
browsers actually do with code.

And, for what it's worth (just for the OP):

aundro@paddy:~$ js
js# 0 == ""
true
js# 0 === ""
false
js#

I personally tend to use the === operator as often as possible.
You should use it only when it is needed.

Too often I see things like this:

typeof o === 'object'

This makes no sense as a typeof operation evaluates to a string
(comparing two strings doesn't involve implicit type conversion.)

However, for example, if a function's argument x can be a string or
null, then strict comparison would be the way to test it:

x === null

If you used a loose comparison on an empty string, you would get the
wrong result.
Jan 16 '08 #6
On Jan 16, 6:13 am, David Mark <dmark.cins...@ gmail.comwrote:
On Jan 16, 4:19 am,
a...@remove.thi s.and.keep.what .follows.ionics oft.com (Arnaud Diederen

(aundro)) wrote:
Randy Webb <HikksNotAtH... @aol.comwrites:
Thomas 'PointedEars' Lahn said the following on 1/15/2008 11:13 PM:
>Java script Dude wrote:
>>Now I can understand that 0 equal false, but should "" also equal
>>false?
>Yes, it should.
>>Apparently the answer is yes. When I test both Firefox and IE, they
>>both say ""==false .
>Works as designed.
>>I assume this goes back to the original spec for JavaScript.
>Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
>implement the ECMAScript Language Specification which says in the Final
>revision of its 3rd Edition:
No, what is more important than what some Specification says is what
browsers actually do with code.
And, for what it's worth (just for the OP):
aundro@paddy:~$ js
js# 0 == ""
true
js# 0 === ""
false
js#
I personally tend to use the === operator as often as possible.

You should use it only when it is needed.

Too often I see things like this:

typeof o === 'object'

This makes no sense as a typeof operation evaluates to a string
(comparing two strings doesn't involve implicit type conversion.)

However, for example, if a function's argument x can be a string or
null, then strict comparison would be the way to test it:

x === null

If you used a loose comparison on an empty string, you would get the
wrong result.
I think it's best to use === by default and == when you specifically
want to be loose. I think more bugs are caused by people using == when
they should be using === than vice versa.

When you say "makes no sense," well, it makes sense to me, because the
programmer is simply saying "compare these things in a strict manner,"
and that's usually what's desired.

You can even have jslint force disallow use of == and !=. That's
pretty harsh, but it probably solves a lot of unexpected misuses of
==. At the least, it makes you consider whether you want strict
comparison or not.

It's safer to err on the side of strict than loose.
Jan 16 '08 #7
timothytoe <ti********@gma il.comwrites:
On Jan 16, 6:13 am, David Mark <dmark.cins...@ gmail.comwrote:
>>
You should use it only when it is needed.

Too often I see things like this:

typeof o === 'object'
What's wrong with that? It just makes it easier for the javascript
engine to do the comparison, as it doesn't even need to check the
types of the two operands.
>>
This makes no sense as a typeof operation evaluates to a string
(comparing two strings doesn't involve implicit type conversion.)

However, for example, if a function's argument x can be a string or
null, then strict comparison would be the way to test it:

x === null

If you used a loose comparison on an empty string, you would get the
wrong result.

I think it's best to use === by default and == when you specifically
want to be loose. I think more bugs are caused by people using == when
they should be using === than vice versa.

When you say "makes no sense," well, it makes sense to me, because the
programmer is simply saying "compare these things in a strict manner,"
and that's usually what's desired.

You can even have jslint force disallow use of == and !=. That's
pretty harsh, but it probably solves a lot of unexpected misuses of
==. At the least, it makes you consider whether you want strict
comparison or not.

It's safer to err on the side of strict than loose.
I agree with timothytoe; being string (in your own code) does not seem
like a bad practice to me.

Best,
A.
Jan 16 '08 #8
I wrote:
I agree with timothytoe; being string (in your own code) does
not seem like a bad practice to me.
Typo. It is, of course, "being strict"

A.
Jan 16 '08 #9
On Jan 16, 10:46*am, timothytoe <timothy...@gma il.comwrote:
On Jan 16, 6:13 am, David Mark <dmark.cins...@ gmail.comwrote:


On Jan 16, 4:19 am,
a...@remove.thi s.and.keep.what .follows.ionics oft.com (Arnaud Diederen
(aundro)) wrote:
Randy Webb <HikksNotAtH... @aol.comwrites:
Thomas 'PointedEars' Lahn said the following on 1/15/2008 11:13 PM:
Java script Dude wrote:
>Now I can understand that 0 equal false, but should "" also equal
>false?
Yes, it should.
>Apparently the answer is yes. When I test both Firefox and IE, they
>both say ""==false .
Works as designed.
>I assume this goes back to the original spec for JavaScript.
Maybe so, but it is more important that JavaScript 1.1+ and JScript1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:
No, what is more important than what some Specification says is what
browsers actually do with code.
And, for what it's worth (just for the OP):
aundro@paddy:~$ js
js# 0 == ""
true
js# 0 === ""
false
js#
I personally tend to use the === operator as often as possible.
You should use it only when it is needed.
Too often I see things like this:
typeof o === 'object'
This makes no sense as a typeof operation evaluates to a string
(comparing two strings doesn't involve implicit type conversion.)
However, for example, if a function's argument x can be a string or
null, then strict comparison would be the way to test it:
x === null
If you used a loose comparison on an empty string, you would get the
wrong result.

I think it's best to use === by default and == when you specifically
want to be loose. I think more bugs are caused by people using == when
they should be using === than vice versa.

When you say "makes no sense," well, it makes sense to me, because the
programmer is simply saying "compare these things in a strict manner,"
and that's usually what's desired.
But the two sides are of equal types. Strict comparison is the same
as loose in this case, so the extra equal sign is unnecessary.
>
You can even have jslint force disallow use of == and !=. That's
pretty harsh, but it probably solves a lot of unexpected misuses of
You can have JSLint do a lot of weird things, including counting
indentation characters.
==. At the least, it makes you consider whether you want strict
comparison or not.
You should certainly always consider that.
>
It's safer to err on the side of strict than loose.
I think an error on either side is just as bad.
Jan 16 '08 #10

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

Similar topics

10
1756
by: Sylvain Thenault | last post by:
Hi there ! Can someone explain me the following behaviour ? >>> l = >>> 0 in (l is False) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iterable argument required >>> (0 in l) is False
1
3920
by: ItsMillerTime4u | last post by:
I'm trying to change the <body> 's event oncontextmenu attributes, but am having no luck at it. I know I can do <body oncontextmenu="contextMenu(); return false;"> but the thing is that I set's this attribute as soon as the page loads......which I don't to happen....I don't want to the context menu visible until the page is done loading, and within the window onLoad event I have a vbscript subroutine that connects to a database to pull...
1
9574
by: yingjian.ma1955 | last post by:
When I want to disable the select functionality, Can I just use onselectstart="return false"? I tried it and it works. But it seems people always put onselectstart="return false" ondragstart="return false" together. What is the use of the latter part?
4
18725
by: Bradley Plett | last post by:
I have what should be a trivial problem. I am using XMLSerializer to serialize an object. It serializes boolean values as "True" and "False". I then want to use an XSLT on this XML, and I want to use one of these booleans in a test. How do I convert one of these to a boolean in a test?!? I know that I could compare it as a string to "True" or "False", but that seems extremely crude to me. The "boolean()" function (which, to me, seems...
3
4545
by: Carpe Diem | last post by:
Hello I have an aspx page that loses Session("user") value after a few minutes even after I set <sessionState mode="InProc" cookieless="false" timeout="300"> in web.config and wrote function Session_Start() { Session.Timeout = 3000; } in global.asax
59
4574
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True So I changed my test for the obvious "if type(v) is bool", but I still find it confusing that "0 in " returns True
13
3995
by: kurtj | last post by:
Hello Gurus: I have a validation script (below) that is somehow messed up. If the Name field is blank, I get the alert message, then the browser window goes to a blank document with the word "false" on it. What the ?!?!?! To test, I commented out the 'return false;' code in the second IF block, so now if there is a value in Name then I get the alert message for Email and the page stays put.
3
3101
by: André | last post by:
Hi, I put that question already, but it's still not very clear to me, so ... Assume following option in web.config= debug="false" but in one aspx page (test.aspx) <%@ debug="true" ..%>
9
11169
by: Jamey Bon | last post by:
As a newbie to C#, I am not sure what I can do about this. I would like to do something like an Enumeration to use "constants" like Yes to indicate true and No for false. But since there seems to be no underlying 0 or non- zero for boolean values in C#, I am not sure how to handle this. Any advice would be appreciated. Thanks, JB
5
8671
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
8265
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.