最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - if(negetive number) is true? Is something wrong with js? - Stack Overflow

programmeradmin1浏览0评论

Is something wrong with js?

if("hello".indexOf("world")) { // I forgot to add > -1 here
    console.log("hello world");
}

Basically if(-1) is true. How is this possible? It took me a whole day to fix this. Is there a list available where these kind of things are listed? Or tools available to catch things like these.

Is something wrong with js?

if("hello".indexOf("world")) { // I forgot to add > -1 here
    console.log("hello world");
}

Basically if(-1) is true. How is this possible? It took me a whole day to fix this. Is there a list available where these kind of things are listed? Or tools available to catch things like these.

Share Improve this question edited Apr 9, 2014 at 16:49 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Apr 7, 2014 at 6:29 bhbbhb 2,5614 gold badges21 silver badges32 bronze badges 2
  • 6 What language are you coming from where negative numbers are false? – david Commented Apr 7, 2014 at 6:52
  • 1 Yes. The 'normal' or 'regular' thing is any real number (negative or positive) is true. Except zero, zero is always false. Although the best way is to look in the language specification to be sure. – marcoslhc Commented Apr 18, 2014 at 3:31
Add a comment  | 

4 Answers 4

Reset to default 12

As per ECMA 5.1 Standard Specifications, the following table is used to determine the truthyness of an expression

+-----------------------------------------------------------------------+
| Argument Type | Result                                                |
|:--------------|------------------------------------------------------:|
| Undefined     | false                                                 |
|---------------|-------------------------------------------------------|
| Null          | false                                                 |
|---------------|-------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion). |
|---------------|-------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;|
|               | otherwise the result is true.                         |
|---------------|-------------------------------------------------------|
| String        | The result is false if the argument is the empty      |
|               | String (its length is zero); otherwise the result is  |
|               | true.                                                 |
|---------------|-------------------------------------------------------|
| Object        | true                                                  |
+-----------------------------------------------------------------------+

The only number that is "falsey" (and would therefore evaluate to false and not pass an 'if' statement) is 0. The rest are "truthy", even negative ones.

You can test this in the console with !!-1. That means converting the value to the Boolean opposite, and repeat once. The first ! on -1 returns false and the second returns true. This is the most common way to convert an expression to its Boolean equivalent.

You can see Truthy and Falsy Values here

The following values are always falsy:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)

All other values are truthy, including "0" (zero in quotes), "false" (false in quotes), empty functions, empty arrays, and empty objects.

As it was mentioned only 0 (considering numbers) is equivalent to zero. But yes there is list of things which are equal to false in javascript and those are:

  1. false
  2. null
  3. undefined
  4. the empty string ""
  5. the number 0
  6. the number NaN

everything else when comapred to false returns false. e.g. -1 == false -> false

发布评论

评论列表(0)

  1. 暂无评论