if(negetive number) is true? Is something wrong with js?

后端 未结 4 1061
广开言路
广开言路 2021-01-26 18:52

Is something wrong with js?

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

Basically

相关标签:
4条回答
  • 2021-01-26 19:23

    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.

    0 讨论(0)
  • 2021-01-26 19:24

    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 讨论(0)
  • 2021-01-26 19:38

    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                                                  |
    +-----------------------------------------------------------------------+
    
    0 讨论(0)
  • 2021-01-26 19:43

    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.

    0 讨论(0)
提交回复
热议问题