Home > Web Front-end > JS Tutorial > 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

伊谢尔伦
Release: 2017-07-18 13:50:57
Original
2127 people have browsed it

Look at the code first:

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

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 || '';
Copy after login

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

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

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"); 
}
Copy after login

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"); 
}
Copy after login

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"); 
}
Copy after login

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('不能为空'); 
}
Copy after login

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('不能为空'); 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template