Home > Web Front-end > JS Tutorial > js judgment is empty Null and string is empty abbreviation method_javascript skills

js judgment is empty Null and string is empty abbreviation method_javascript skills

WBOY
Release: 2016-05-16 16:58:30
Original
1742 people have browsed it

Recently, I suddenly found that the JavaScript code I wrote was bloated, so I started to study the abbreviation method of JavaScript. In this way, our JavaScript code can look cleaner, and it can also improve our technology. So how to abbreviate the judgment of empty?
The following is the abbreviation of the judgment of empty.
The code is as follows

Copy codeThe code is as follows:

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

The above means that if variable1 is not an empty object, Either it is undefined, or it 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.
Abbreviation code:
The code is as follows
Copy codeThe code is as follows:

var variable2 = variable1 || '';

The following is the incorrect method:
The code is as follows
Copy code 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 different. Note: This method can be used when you want to judge null and undefined at the same time.
The code is as follows
Copy code The code is as follows:

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

If exp is undefined, or the number zero, or false, you will also get the same as null has the same result, although null and both are different. 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
Copy code 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
Copy code The code is as follows:

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

Check whether the string is empty
s match 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
Copy code The code is as follows:

var strings = '';
if (string.length == 0)
{
alert('cannot be empty');
}

But what if the user inputs spaces, tabs, or form feeds? In this case, it is 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
Copy code The code is as follows:

var strings = ' ';
if (strings.replace(/(^s*)|(s*$)/g, "").length = =0)
{
alert('cannot be empty');
}

s The lowercase s is, matches any whitespace character, including spaces, tabs, Form breaks, etc. Equivalent to [fnrtv].
This is the abbreviation for the judgment that it is empty. I hope the above method can be helpful to you.
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