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

后端 未结 4 1124
广开言路
广开言路 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: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                                                  |
    +-----------------------------------------------------------------------+
    

提交回复
热议问题