JS six Big Data types: Number, String, object, Boolean, null, undefined
String: Illustrated by single or double quotation marks, such as "string"
Number: What integer AH floating point is called numbers, you know ~
Boolean: True and False.
Undefined: Undefined, when you create a variable without assigning it a value ~
Null: So the name is long, NULL is not, nothing is said
object: I can hardly explain that. Except for the five types above.
--------------------above are clouds, the following is the god horse------------------------------
typeof of data type judgment
typeof can solve most of the data type judgment, is a unary operation, placed before an operation value, its return value is a string that describes the type of the operand, so to determine whether a string type, you can direct if (typeof (your value) = = "string") {}
The following are the various data types that return results:
var a= "string"; 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 also determine the type of function in addition to the data type
This makes it obvious that, except for the first four types, null, object, and array are returned as object types;
Functions are returned for the function type, such as typeof (Date), typeof (Eval), and so on.
And then here you can extend another gray hot and the solution has a common problem, how to determine the data is the array type?
---------------------------------------actually this is my purpose, Baa ~----------------------------------------------
js method for judging array types
Instanceof of Method One
instance, hence the name of righteousness, examples, examples, so instanceof used to determine whether a variable is an instance of an object, is a three-mesh operation---and typeof the most substantial difference
A instanceof B?alert ("true"): Alert ("false")//Note that the B-value is the type of data you want to judge, not a string, such as an array
Give me a chestnut:
var a=[];console.log (a instanceof Array)//return True
Constructor of Method Two
Definition in the definition: constructor property returns a reference to the array function that created this object
Is the constructor that corresponds to the return object. It's not exactly the same as instanceof in terms of definition, but the effect is the same.
such as: (a instanceof Array)//a is an instance of array? True or False
(A.constructor = = array)//A constructor for an instance is an array? True or False
Give me a chestnut:
function Employee (name,job,born) { this.name=name; This.job=job; This.born=born;} VAR bill=new employee ("Bill Gates", "Engineer", 1985); Console.log (Bill.constructor); Output function employee (name, JobTitle, born) {this.name = name; this.jobtitle = Job; This.born = born;}
Then the method of judging various types is:
Console.log ([].constructor = = Array); Console.log ({}.constructor = = Object); Console.log ("string". Constructor = = String); Console.log ((123). constructor = = number); Console.log (true.constructor = = Boolean);
-------------------------------------below are not original--------------------------------------
A more rigorous and versatile approach:
function IsArray (object) { return object && typeof object=== ' object ' && Array = = Object.constructor;}
!! Attention:
With Instaceof and Construcor, the determined array must be declared on the current page! For example, a page (parent page) has a frame, the frame refers to a page (sub-page), in the Sub-page declaration of an Array, and assign it to the parent page of a variable, then determine the variable, Array = = Object.constructor; returns false;
Reason:
1, array belongs to the reference data, in the process of delivery, is simply the delivery of the reference address.
2, each page of the array native object refers to the address is not the same, in the sub-page declaration of the array, the corresponding constructor is a sub-page array object, the parent page to judge, the use of the array is not equal to the sub-page array; Remember, it's hard to track the problem!
The method of characteristic judgment of three methods
The above methods have some defects, but to believe that the wisdom of the masses is omnipotent, we can judge its type according to some characteristics of the array.
function IsArray (object) { return object && typeof object=== ' object ' && typeof object.length=== ' number ' && typeof object.splice=== ' function ' && //Determine if the length property is enumerable for arrays Will get false ! ( Object.propertyisenumerable (' length '));}
Having length and splice is not necessarily an array, because you can add attributes to an object, but you cannot enumerate the length property, which is the most important factor to judge.
PS: Here to popularize the propertyIsEnumerable method:
Object. propertyIsEnumerable (Proname)
Determines whether the specified property can be enumerated
Note: If proname exists in object and you can use a for ... In cyclic poor lift, then the propertyIsEnumerable property returns True. The propertyIsEnumerable property returns False if object does not have the specified property, or if the specified property is not enumerable.
The propertyIsEnumerable property does not take into account objects in the prototype chain.
Example:
var a = new Array ("Apple", "banana", "Cactus");d Ocument.write (a.propertyisenumerable (1));
Method four The simplest method
For this approach, here are a few links to explain:
http://blog.csdn.net/zhangw428/article/details/4171630
http://my.oschina.net/sfm/blog/33197
http://openxtiger.iteye.com/blog/1893378
function IsArray (o) { return Object.prototype.toString.call (o) = = = ' [Object Array] ';}
"Reprint" http://www.cnblogs.com/mofish/p/3388427.html
JS data type judgment and array judgment