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

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

提交回复
热议问题