Home  >  Article  >  Web Front-end  >  Detailed explanation of the abbreviation method example of js to determine whether it is an empty string

Detailed explanation of the abbreviation method example of js to determine whether it is an empty string

伊谢尔伦
伊谢尔伦Original
2017-07-18 13:50:571928browse

Look at the code first:

if (variable1 !== null || variable1 !== undefined || variable1 !== '') { 
var variable2 = variable1; 
}

The above means that if variable1 is not an empty object, or is undefined, or is not equal to the empty string, then declare a variable2 variable and assign variable1 to variable2. That is to say, if variable1 exists, then the value of variable1 is assigned to variable2, if it does not exist, it is an empty string. Such as the following abbreviated code.
Abbreviated code:
The code is as follows

var variable2 = variable1 || '';

The following is an incorrect method:
The code is as follows

var exp = null; 
if (exp == null) { alert("is null"); }

When exp is undefined, the same result as null will be obtained. , although null and undefined are not the same. Note: This method can be used when you want to judge null and undefined at the same time.
The code is as follows

var exp = null; 
if (!exp) 
{ 
alert("is null"); 
}

If exp is undefined, or the number zero, or false, the same result as null will be obtained, although null is different from the two. Note: This method can be used when you want to judge null, undefined, number zero, and false at the same time.
The code is as follows

var exp = null; 
if (typeof exp == "null") 
{ 
alert("is null"); 
}

For backward compatibility, when exp is null, typeof null always returns object, so it cannot be judged this way.
The code is as follows

var exp = null; 
if (isNull(exp)) 
{ 
alert("is null"); 
}

Determine whether the string is empty
s matches any whitespace characters, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv]. In many cases, length is used to directly determine whether a string is empty, as follows:
The code is as follows

var strings = ''; 
if (string.length == 0) 
{ 
alert('不能为空'); 
}

But what if the user inputs spaces, tabs, or form feeds? In this case , is also not empty, but such data is not what we want.
In fact, you can use regular expressions to remove these "empty" symbols for judgment.
The code is as follows

var strings = ' '; 
if (strings.replace(/(^s*)|(s*$)/g, "").length ==0) 
{ 
alert('不能为空'); 
}

s The lowercase s matches any whitespace character, including spaces and tabs. , page feed, etc.

The above is the detailed content of Detailed explanation of the abbreviation method example of js to determine whether it is an empty string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn