开发者

Javascript: How to check if a string is empty? [duplicate]

开发者 https://www.devze.com 2022-12-22 02:49 出处:网络
This question already has answers her开发者_StackOverflow中文版e: How do I check for an empty/undefined/null string in JavaScript?
This question already has answers her开发者_StackOverflow中文版e: How do I check for an empty/undefined/null string in JavaScript? (52 answers) Closed 2 years ago.

I know this is really basic, but I am new to javascript and can't find an answer anywhere.

How can I check if a string is empty?


I check length.

if (str.length == 0) {
}


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.

for example: (false == "") will return true, and (false === "") will return false.


This should work:

if (variable === "") {

}


But for a better check:

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


if (value == "") {
  // it is empty
}
0

精彩评论

暂无评论...
验证码 换一张
取 消