Written at the beginning:
Yesterday, I found that a very simple question was not answered. Maybe it was because I was too nervous and I felt that I was just crying stupidly. Later I thought about it and I should have recorded it carefully so that I can be impressed. The revolution has not been successful yet, and it still needs to be done to become strong!
1. JS six major data types
number: numbers, integers, floating point numbers, etc.
string: single or double quotes to illustrate.
Boolean: Return true and false, these two values do not necessarily correspond to 1 and 0
object: object, you can execute the new operator followed by the name of the object type to be created to create.
null: There is only one worthy data type, logically speaking, the null value represents an empty object pointer.
undefined: Undefined. When a variable is declared using var but not initialized, the value of the variable is undefined.
2. Typeof data type judgment
typeof can solve the judgment of most data types, and its return value is a string, which indicates the type of the operand.
//Judge whether the variable num is a numeric type if(typeof num=='number') { return true;}Return result:
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 In addition to determining data types, you can also determine function typesIn addition to the four types of string, number, boolean, and undefined, null, object, and array return all object types! ! !
For function types, the function is returned, such as typeof(Date), typeof(eval), etc.
3. JS method to determine array type
1) instanceof
instanceof is used to determine whether a variable is an instance of an object. It is a trilogy operation formula. This operator has something to do with object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. Because this operator detects whether the prototype chain of the object points to the prototype object of the constructor.
a instanceof b?alert("true"):alert("false") //Note that the b value is the data type you want to judge, is it a string, such as Arrayexample:
var arr = [1,2,3,1]; alert(arr instance of Array); // true
2) constructor
Definition in W3C 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);General method:
function isArray(object){ return object && typeof object==='object' && Array == object.constructor;}3) Characteristic judgment
object.isArray() to determine, the purpose is to accurately detect whether a value is an array. IE9+, Firefox 4+, Safari 5+, Opera 10.5+, and Chrome all implement this method. However, it is not supported before IE8.
function isArray(object){ return object && typeof object==='object' && typeof object.length==='number' && typeof object.splice==='function' && //Just determine whether the length property is enumerable for arrays, false will be obtained!(object.propertyIsEnumerable('length'));}4) Object.prototype.toString.call
Object.prototype.toString.call(value) == '[object Array]'
The above brief discussion on js data type judgment and array judgment is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.