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

后端 未结 4 1066
广开言路
广开言路 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题