if(negetive number)是真的吗? js有问题吗? [英] if(negetive number) is true? Is something wrong with js?

查看:120
本文介绍了if(negetive number)是真的吗? js有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

js有问题吗?

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

基本上 if(-1)是真的。这怎么可能?我花了一整天才解决这个问题。是否列出了列出这些类型的列表?或者可以用来捕捉这些东西的工具。

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.

推荐答案

根据 ECMA 5.1标准规范,下表用于确定表达式的真实性

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                                                  |
+-----------------------------------------------------------------------+

这篇关于if(negetive number)是真的吗? js有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆