Javascript:如何检查字符串是否为空?

时间:2010-03-04 17:40:39

标签: javascript string

  

可能重复:
  What is the best way to check for an empty string in JavaScript?

我知道这是非常基本的,但我是javascript的新手,无法在任何地方找到答案。

如何检查字符串是否为空?

5 个答案:

答案 0 :(得分:121)

我检查长度。

if (str.length == 0) {
}

答案 1 :(得分:44)

如果你想知道它是否为空字符串,请使用===而不是==。

if(variable === "") {
}

这是因为如果两边的值都是相同的类型,===只会返回true,在这种情况下是一个字符串。

例如: (false ==“”)将返回true,(false ===“”)将返回false。

答案 2 :(得分:8)

这应该有效:

if (variable === "") {

}

答案 3 :(得分:8)

但为了更好的检查:

if(str === null || str === '')
{
    //enter code here
}

答案 4 :(得分:-5)

if (value == "") {
  // it is empty
}
相关问题