javascript - 我如何与空变量进行比较?

标签 javascript is-empty appconkit

如果我将一个变量设置为 0,我会得到一个奇怪的行为,即与“”(空)的比较为真。如何检查变量是否真的为空?

tmp = 0;

if ( tmp != "")
{
    //do something - This is where the code goes.
}
else
{
   //isEmpty - I would expect to be here
}

最佳答案

使用strict comparison operators ===!==

使用 ==!=(称为抽象比较运算符),

If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison.


如果为空,你想检查变量是否没有被定义,使用:

if (typeof tmp !== "undefined") {
    // it exists!
}

关于javascript - 我如何与空变量进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16056476/

相关文章:

javascript - 如何让 Protractor 按下ESCAPE键?

javascript - 为 WordPress 主题添加 JavaScript 功能

javascript - 在 nodemailer 中,如何格式化文本电子邮件正文

javascript - 删除空 <p>,但每组仅允许一个

php - 如何检查多维数组是否仅包含空值?

php empty() 无法正常工作

javascript - 应用ConKit3 : Not possible to dismiss current view in onFinish Callback

javascript - 滑动菜单一下子全部出来