Write at the beginning:
Yesterday the interview found a very very simple question unexpectedly did not answer up, probably also is because of too nervous, feel stupid cry. Then think about it or should be carefully recorded, so as to be impressed. The revolution has not yet succeeded, the sturdy still needs diligently!
1. JS six major data types
Number: numbers, integers, floating points, and so on,
String: Single or double quotes to illustrate,
Boolean: Returns True and false, these values do not necessarily correspond to 1 and 0
Object: Objects that can be created by executing the new operator followed by the name of the object type to be created.
Null: Only one worthy data type, logically, a null value represents an empty object pointer.
Undefined: Undefined, the value of the variable is undefined when the variable is declared with Var but not initialized.
2. typeof of data type judgment
typeof can resolve most data types, and its return value is a string that describes the type of operand.
Determines whether the variable num is a numeric type
if (typeof num== ' number ') {return
true;
}
return Result:
var a= "hling"; Console.log (a); String
var a=1; Console.log (a);//number
var a=false; Console.log (a);//boolean
var A; Console.log ( typeof a); Undfined
var a = null; Console.log (typeof a);//object
var a = document; Console.log (typeof a);//object
var a = []; Console.log (a); Object
var a = function () {}; Console.log (typeof a)//function can determine the type of a
function in addition to the data type
In addition to the four types of string, number, Boolean, and Undefined, null, object, array return are object types!!!
Functions are returned for function types, such as typeof (Date), typeof (Eval), and so on.
3. js method of judging array type
1) instanceof
Instanceof is used to determine whether a variable is an instance of an object, a three-mesh expression. This operator has something to do with JavaScript-oriented objects, so you need to understand the object-oriented in JavaScript first. Because this operator is the prototype object that detects whether the object's prototype chain points to the constructor.
A instanceof B?alert ("true"): Alert ("false")
//Note that the B value is the type of data you want to judge, is not a string, such as an array
Example:
2) constructor
Definition in the Consortium definition: constructor property returns a reference to the array function that created this object
var arr = [];
Arr instanceof Array; True
Arr.constructor = = Array;//true
The methods to judge 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) characteristic judgment
Object.isarray () to determine whether a value is accurately detected as an array. ie9+, Firefox 4+, Safari 5+, Opera 10.5+, and chrome all implement this approach. However, the version prior to IE8 is not supported.
function IsArray (object) {return
object && typeof object=== ' object ' &&
typeof object.length== = ' number ' &&
typeof object.splice=== ' function ' &&
//Determine whether the length property is enumerable for the array will get false
! Object.propertyisenumerable (' length '));
4) Object.prototype.toString.call
Object.prototype.toString.call (value) = = ' [Object Array] '
The above discussion JS data type judgment and the array judgment is small arranges to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.