About js data type judgment and array judgment, and about js data type array

Source: Internet
Author: User

About js data type judgment and array judgment, and about js data type array

Preface:

Yesterday's interview found that a very simple question was not answered, probably because it was too nervous and I felt dumbfounded. Later, I think we should record it carefully to be impressed. The revolution is not yet successful, and we still need to work hard!

1. js six Data Types

Number: number, integer, floating point number, etc,

String: single or double quotation marks,

Boolean: returns true and false. These two values do not necessarily correspond to 1 and 0.

Object: object. You can execute the new operator and create it with the name of the object type to be created.

Null: only one data type is worth it. Logically, null indicates a null object pointer.

Undefined: undefined. When a variable is declared with var but not initialized, the value of the variable is undefined.

2. typeof data type determination

Typeof can be used to determine most data types. Its return value is a string, which indicates the type of the number of operations.

// Judge whether the variable num is a numeric type if (typeof num = 'number') {return true ;}

Returned results:

Var a = "hling"; console. log (a); // stringvar a = 1; console. log (a); // numbervar a = false; console. log (a); // booleanvar a; console. log (typeof a); // undfinedvar a = null; console. log (typeof a); // objectvar a = document; console. log (typeof a); // objectvar a = []; console. log (a); // objectvar a = function () {}; console. log (typeof a) // function can be used to determine the data type and function type.

Except for four types: string, number, boolean, and undefined, null, object, and array all return object types !!!

Function is returned for the function type, such as typeof (Date) and typeof (eval.

3. js method for judging array types

1) instanceof

Instanceof is an example used to determine whether a variable is an object. This operator has a relationship with object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. This operator is used to check whether the prototype object of an object is directed to the prototype object of the constructor.

A instanceof B? Alert ("true"): alert ("false") // note that the value of B is the type of data you want to determine, whether it is a string, such as Array

Example:

var arr = [1,2,3,1]; alert(arr instanceof Array); // true 

2) constructor

Definition in W3C definition: the constructor attribute returns a reference to the array function that creates this object.

var arr = []; arr instanceof Array; // true arr.constructor == Array; //true

The methods to determine various types are:

console.log("string".constructor == String);console.log((123).constructor == Number);console.log(false.constructor == Boolean);console.log([].constructor == Array);console.log({}.constructor == Object);

Common Methods:

function isArray(object){  return object && typeof object==='object' &&      Array == object.constructor;}

3) feature judgment

Object. isArray () to determine whether a value is an array. IE9 +, Firefox 4 +, Safari 5 +, Opera 10.5 +, and Chrome all implement this method. However, versions earlier than IE8 are not supported.

Function isArray (object) {return object & typeof object === 'object' & typeof object. length = 'number' & typeof object. splice === 'function' & // determines whether the length attribute is enumerable. If the array is set to false! (Object. propertyIsEnumerable ('length '));}

4) Object. prototype. toString. call

Object.prototype.toString.call(value) == '[object Array]'

The above discussion about js data type judgment and array judgment is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the customer's house.

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.