Javascript code used to determine whether the parameter (String, Array, Object) is undefined or the value is empty _ javascript

Source: Internet
Author: User
When some front-end controls need to submit data to the server for data verification, they need to determine whether the submitted data is empty. For string data of a common form, you only need to judge the length after trim, and the data needed here can be of different types, through JSON. when stringify (data) is serialized before being passed, sometimes we encounter this situation: when some front-end controls need to submit data to the server for data verification, you need to determine whether the submitted data is empty. For string data of a common form, you only need to judge the length after trim, and the data needed here can be of various types (numbers, strings, arrays, objects, etc ), JSON. stringify (data) is serialized before being passed.

The following data values are defined as "null ":

• Undefined
• Null
• Empty strings and pure blank strings: ''and.
• Empty array: []
• Empty object :{}

Other data values are considered as not null.

Among them, null and undefined are easy to recognize, but for other types, we need to obtain their data types to use the corresponding method to detect whether the data is empty. The easiest way to think of is to use the typeof OPERATOR:

The Code is as follows:


If (typeof data = 'number '){
// Deal with numbers
}


However, typeof only returns 'object', 'function', 'number', 'boolean', 'string', and 'undefined' types, many native objects such as Date and RegExp cannot be distinguished from objects created. In addition, for some basic data types such as String, Number, and Boolean, typeof will return different values for their corresponding BASIC packaging data, such:

The Code is as follows:


Console. log (typeof false); // 'boolean'
Console. log (typeof new Boolean (false); // 'object'
Console. log (typeof 1); // 'number'
Console. log (typeof new Number (1); // 'object'
Console. log (typeof ''); // 'string'
Console. log (typeof new String (''); // 'object'


This also has some impact on our judgment.

Use instanceof? This only determines the object. When multiple frames exist, multiple similar objects do not share the prototype. The objects obtained from other frames cannot be correctly determined.

Fortunately, there is also the simplest and most reliable method: Object. prototype. toString. For different types of data, this method can return strings such as '[object Object]', '[object Array]', and '[object String]', which is very convenient for judgment. It should be noted that in IE8 and earlier browsers, this method will return '[ object Object] 'for null, undefined, window, and so on, but fortunately, this does not affect the use of IT to determine the null object.

The following code is directly used to describe it.

The Code is as follows:


Var isEmptyValue = function (value ){

Var type;
If (value = null) {// equivalent to value = undefined | value = null
Return true;
}
Type = Object. prototype. toString. call (value). slice (8,-1 );
Switch (type ){
Case 'string ':
Return! $. Trim (value );
Case 'array ':
Return! Value. length;
Case 'object ':
Return $. isEmptyObject (value); // you can use for... in to determine a common object. If a key is set to false
Default:
Return false; // other objects are considered non-empty
}
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.