Possible Duplicate:
What is the best way to check for an empty string in JavaScript?可能重复:在JavaScript中检查空字符串的最佳方法是什么?
I know this is really basic, but I am new to javascript and can't find an answer anywhere.
我知道这是非常基本的,但我是javascript的新手,无法在任何地方找到答案。
How can I check if a string is empty?
如何检查字符串是否为空?
5 个解决方案
#1
112
I check length.
我检查长度。
if (str.length == 0) {
}
#2
39
If you want to know if it's an empty string use === instead of ==.
如果你想知道它是否为空字符串,请使用===而不是==。
if(variable === "") {
}
This is because === will only return true if the values on both sides are of the same type, in this case a string.
这是因为如果两边的值都是相同的类型,===只会返回true,在这种情况下是一个字符串。
for example: (false == "") will return true, and (false === "") will return false.
例如:(false ==“”)将返回true,而(false ===“”)将返回false。
#3
8
This should work:
这应该工作:
if (variable === "") {
}
#4
7
But for a better check:
但为了更好的检查:
if(str == "" || str == null)
{
//enter code here
}
#5
-4
if (value == "") {
// it is empty
}
#1
112
I check length.
我检查长度。
if (str.length == 0) {
}
#2
39
If you want to know if it's an empty string use === instead of ==.
如果你想知道它是否为空字符串,请使用===而不是==。
if(variable === "") {
}
This is because === will only return true if the values on both sides are of the same type, in this case a string.
这是因为如果两边的值都是相同的类型,===只会返回true,在这种情况下是一个字符串。
for example: (false == "") will return true, and (false === "") will return false.
例如:(false ==“”)将返回true,而(false ===“”)将返回false。
#3
8
This should work:
这应该工作:
if (variable === "") {
}
#4
7
But for a better check:
但为了更好的检查:
if(str == "" || str == null)
{
//enter code here
}
#5
-4
if (value == "") {
// it is empty
}