Sie sind auf Seite 1von 7

1/21/14

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow
sign up log in tour help careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Take the 2-minute tour

In Javascript, why is 0 equal to false, but not false by itself?

The following shows that " 0 " is false in Javascript: > > >" 0 "= =f a l s e t r u e > > >f a l s e= =" 0 " t r u e So why does the following print " h a "? > > >i f( " 0 " )c o n s o l e . l o g ( " h a " ) h a
javascript boolean

edited Oct 7 '11 at 1:26 Kip 32.8k 53 135 185

asked Sep 30 '11 at 19:34 28.1k 26 180 378

14 3 2 3 2

" 0 " is a string, and since it's not empty, it's evaluated to true. Digital Plane Sep 30 '11 at 19:36 " 0 "= = =f a l s e[ . . . ]f a l s e user1385191 Sep 30 '11 at 19:36 Check out Angus Croll's article truth in javascript. javascriptweblog.wordpress.com/2011/02/07/ timrwood Sep 30 '11 at 20:25 ' 0 ' = = f a l s e but '0' is not a falsey value (yes Javascript can be weird) Linsey Sep 30 '11 at 23:25 @Linsey: The whole "falsy" and "truthy" thing was only ever meant to explain how values are converted to booleans. When you compare two values with = =, they never get converted to booleans, so it doesn't apply. (The rules for the conversion seem to favour converting to numbers.) millimoose Oct 1 '11 at 0:56

show 3 more comments

11 Answers
The reason is because when you explicitly do " 0 "= =f a l s e, both sides are being converted to numbers and then the comparison is performed. When you do: i f( " 0 " )c o n s o l e . l o g ( " h a " ), the string value is being tested. Any non-empty string is true, while an empty string is false. 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. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory. (From Comparison Operators in Mozilla Developer Network)

stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself

1/7

1/21/14
edited Aug 10 '13 at 22:32 totymedli 2,913 2 15 37 add comment

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow
answered Sep 30 '11 at 19:46 jdi 33.7k 2 30 63

Tables displaying the issue:

and ==

Moral of the story use ===

stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself

2/7

1/21/14

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow

table generation credit: https://github.com/dorey/JavaScript-Equality-Table


edited Oct 11 '11 at 16:39 answered Sep 30 '11 at 19:36 Joe 31.4k 5 44 75

8 3 1

-1: Missed the point entirely, IMO. Lightness Races in Orbit Sep 30 '11 at 20:20 At least it's a colorful picture. aziz punjani Sep 30 '11 at 20:49 Note: Just learn the rules it's easier to remember than a massive grid. Side-Note. Rendering up at the top goofed a bit. Incognito Sep 30 '11 at 20:53

10 Yeah but grids are more fun, especially colorful grids. aziz punjani Sep 30 '11 at 21:02 1
Better now; the diagram addresses the point. Lightness Races in Orbit Oct 1 '11 at 14:01

show 2 more comments

It's according to spec. 1 2 . 5T h ei fS t a t e m e n t . . . . . 2 .I fT o B o o l e a n ( G e t V a l u e ( e x p r R e f ) )i st r u e ,t h e n a .R e t u r nt h er e s u l to fe v a l u a t i n gt h ef i r s tS t a t e m e n t . 3 .E l s e , . . . .

stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself

3/7

1/21/14

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow
ToBoolean, according to the spec, is The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11: And that table says this about strings:

The result is false if the argument is the empty String (its length is zero); otherwise the result is true Now, to explain why " 0 "= =f a l s e you should read the equality operator, which states it gets its value from the abstract operation G e t V a l u e ( l r e f ) matches the same for the right-side. Which describes this relevant part as:

i fI s P r o p e r t y R e f e r e n c e ( V ) ,t h e n a .I fH a s P r i m i t i v e B a s e ( V )i sf a l s e ,t h e nl e tg e tb et h e[ [ G e t ] ]i n t e r n a lm e t h o do fb a s e ,o t h e r w i s el e tg e t b et h es p e c i a l[ [ G e t ] ]i n t e r n a lm e t h o dd e f i n e db e l o w . b .R e t u r nt h er e s u l to fc a l l i n gt h eg e ti n t e r n a lm e t h o du s i n gb a s ea si t st h i sv a l u e ,a n dp a s s i n g G e t R e f e r e n c e d N a m e ( V )f o rt h ea r g u m e n t Or in other words, a string has a primitive base, which calls back the internal get method and ends up looking false. If you want to evaluate things using the GetValue operation use = =, if you want to evaluate using the T o B o o l e a n, use = = = (also known as the "strict" equality operator)
edited Oct 10 '12 at 9:00 NullUserException 39.6k 10 82 154 answered Sep 30 '11 at 19:47 Incognito 10k 3 34 82

" as t r i n gh a sap r i m i t i v eb a s e ,w h i c hc a l l sb a c kt h ei n t e r n a lg e tm e t h o da n de n d su p l o o k i n gf a l s e " Is this true for all strings ? aziz punjani Sep 30 '11 at 20:26 @Interstellar_Coder S e c t i o n8 . 1 2 . 3 :[ [ G e t ] ]( P ) describes how it works. It's true only for cases that string are 0, as it does a bunch of other internal calls eventually resulting in G e t O w n P r o p e r t y which sees that "whatever" is a data property, which then returns all the way back that value. This is why "0" is false, and "blah" is true. Check out some of Douglas Crockford's videos on Yahoo developer theater, he describes "truthyness" in JavaScript a little less complex than I am. If you understand what "truthy" and "falsy" means you'll understand Bobince's answer right away. Incognito Sep 30 '11 at 20:46 +1 good explination rlemon Nov 2 '11 at 15:31 add comment

When running an i f statement, the parameter is converted to a Boolean primitive, essentially like the following:

stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself

4/7

1/21/14
! ! 0 / / >f a l s e ! ! " 0 " / / >t r u e

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow

0 is a Number, while " 0 " is a String. Strings of any length except for length 0 are truthy values, i.e. all strings are truthy except for the empty string. in this case " 0 "! = = 0. All numbers in JavaScript are truthy except for 0 and N a N. " 0 "= =f a l s e turns both operands into numbers, just as " 3 "= = 3 does (it returns t r u e).
edited Oct 6 '11 at 23:42 Joe 31.4k 5 44 75 answered Sep 30 '11 at 19:38 kzh 4,202 2 22 49

-1: Missed the point entirely, IMO. Lightness Races in Orbit Sep 30 '11 at 20:19 Not quite what the question was about, what about the " 0 " = = f a l s e question? Deleteman Sep 30 '11 at 20:39

"All numbers in JavaScript are truthy except for 0." I would add N a N, since despite its name ("not a number"), it's a value of the Number type ( t y p e o fN a N= =' n u m b e r '). CMS Sep 30 '11 at 20:59

add comment

It's PHP where the string " 0 " is falsy (false-when-used-in-boolean-context). In JavaScript, all non-empty strings are truthy. The trick is that = = against a boolean doesn't evaluate in a boolean context, it converts to number, and in the case of strings that's done by parsing as decimal. So you get Number 0 instead of the truthiness boolean t r u e. This is a really poor bit of language design and it's one of the reasons we try not to use the unfortunate = = operator. Use = = = instead.
answered Sep 30 '11 at 19:46 bobince 244k 48 312 529 +1 for saying "Use = = = instead". Robbie JW Nov 2 '13 at 14:05 add comment

Your quotes around the 0 make it a string, which is evaluated as true. Remove the quotes and it should work. i f( 0 )c o n s o l e . l o g ( " h a " )
answered Sep 30 '11 at 19:37 Jason Gennaro 19.3k 3 14 40

-1: Missed the point entirely, IMO. Lightness Races in Orbit Sep 30 '11 at 20:19

add comment

It is all because of the ECMA specs ... " 0 "= =f a l s e because of the rules specified here http://ecma262-5.com/ELS5_HTML.htm#Section_11.9.3 ...And i f( ' 0 ' ) evaluates to true because of the rules specified here http://ecma262-5.com/ELS5_HTML.htm#Section_12.5
answered Sep 30 '11 at 20:10 Narendra Yadala 5,222 1 7 23 I didn't know someone ported the spec to a site... that's awesome! No more PDFs for me. Incognito Sep 30 '11 at 20:54 yes, it is really good indeed for linking purposes.. Narendra Yadala Oct 1 '11 at 4:35 add comment

stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself

5/7

1/21/14

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow

The "if" expression tests for truthiness, while the double-equal tests for type-independent equivalency. A string is always truthy, as others here have pointed out. If the double-equal were testing both of its operands for truthiness and then comparing the results, then you'd get the outcome you were intuitively assuming, i.e. ( " 0 "= =t r u e )= = =t r u e. As Doug Crockford says in his excellent JavaScript: the Good Parts, "the rules by which [== coerces the types of its operands] are complicated and unmemorable.... The lack of transitivity is alarming." It suffices to say that one of the operands is typecoerced to match the other, and that "0" ends up being interpreted as a numeric zero, which is in turn equivalent to false when coerced to boolean (or false is equivalent to zero when coerced to a number).
answered Sep 30 '11 at 19:49 Jollymorphic 2,665 4 9 add comment

i f( x ) coerces x using JavaScript's internal toBoolean (http://es5.github.com/#x9.2) x= =f a l s e coerces both sides using internal toNumber coercion (http://es5.github.com/#x9.3) or toPrimitive for objects (http://es5.github.com/#x9.1) For full details see http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/
answered Oct 1 '11 at 0:08 AngusC 351 2 7 add comment

" 0 " is a string, not a number so it will give you result h a . In javaScript there are only six falsy values, 1. 2. 3. 4. 5. 6. false null undefined number 0 NaN the empty string.

and also javaScript is dynamically typed and when used with some operator or comparison it follow strict type conversion rule.If one operand is n u m b e r or B o o l e a n than it convert both side into number and than compare it else it will try to convert operands into string. .So when you are comparing it will give you true result because " 0 " and f a l s e both converted into number 0 but when you are checking for a condition it will check for a string " 0 " which is a truthy value, so execute the block.
edited Feb 15 '13 at 8:14 answered Feb 15 '13 at 8:06 pandit 748 5 16

add comment

/ /Iu s u a l l yd ot h i s : x=" 0 "; i f( ! ! + x )c o n s o l e . l o g ( ' Ia mt r u e ' ) ; e l s e c o n s o l e . l o g ( ' Ia mf a l s e ' ) ; / /E s s e n t i a l l yc o n v e r t i n gs t r i n gt oi n t e g e ra n dt h e nb o o l e a n .


answered Dec 20 '13 at 1:20 Thava 333 3 6 add comment

Not the answer you're looking for? Browse other questions tagged javascript boolean or ask your own question.
stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself 6/7

1/21/14

boolean - In Javascript, why is "0" equal to false, but not false by itself? - Stack Overflow

stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself

7/7

Das könnte Ihnen auch gefallen