如何在JavaScript中检查empty / undefined / null字符串? [英] How to check empty/undefined/null string in JavaScript?

查看:121
本文介绍了如何在JavaScript中检查empty / undefined / null字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这个主题,但我没有看到JavaScript特定的例子。在JavaScript中是否有一个简单的 string.Empty ,或者只是检查

I saw this thread, but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?

推荐答案

如果你只想检查是否有任何价值,你可以这样做

If you just want to check whether there's any value, you can do

if (strValue) {
    //do something
}

如果您需要专门检查空字符串是否超过null,我认为检查是最好的选择,使用 === 运营商(以便你知道它实际上是一个你要比较的字符串。)

If you need to check specifically for an empty string over null, I would think checking against "" is your best bet, using the === operator (so that you know that it is, in fact, a string you're comparing against).

if (strValue === "") {
    //...
}

这篇关于如何在JavaScript中检查empty / undefined / null字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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