Please indicate the source of reprint
The original connection blog.huanghanlian.com/article/5b6…
The data type
JavaScript has six data types
JavaScript has six data types, five primitive types and one object type.
JavaScript implicit conversion
var x='The answer'+42;//The answer42
var y=42+'The answer';//42The answer
Copy the code
The plus sign here can be understood as a concatenation of strings
var x="37"7 -; / / 30
var y="37"+7; / / 377
Copy the code
The minus sign here will be understood as subtraction, and the plus sign as string concatenation
Is equal to the judge
var x="1.23"= =1.23;//true Attempts to convert strings to numbers for comparison when equal is equal to one string and one number
var y=0= =false;//true
var e=null= =undefined;//true
var c=new Object() = =new Object(a);//false
var d=[1.2] = = [1.2];//false
Copy the code
Different types, try conversion and comparison
Null == undefined number == String turn number 1 == “1.0” // true Boolean ==? Turn number 1 = = true / / true object = = number | try string objects into the basic types of new string (‘ hi ‘) = = ‘hi’ / / other: true or false
Strictly equal to the
A ===b as the name suggests, it first checks the type on both sides of the equals sign, returns false if the two sides are of different types if they are of the same type, same type ===
var h=null= = =null;//true
var f=undefined= = =undefined;//true
var g=NaN= = =NaN;// False number type it is not equal to any value, including itself
var l=new Object() = = =new Object(a);// False objects should be compared by reference, not by value, because they are not identical objects. So we define objects x and y to compare and only then will it be true
Copy the code
JavaScript wrapped objects
Primitive types Number, String, and Boolean all have corresponding wrapper types.
varA = "string". alert(a.length);/ / 6
a.t = 3;
alert(a.t);//undefined
Copy the code
String, when trying to use a primitive type as an object, such as accessing the length property, JS will be smart to convert the primitive type to the corresponding wrapper type object. That’s new string. When the access is complete, the temporary object is destroyed. So the value of a.t is set to 3 and then the output of a.t is undefined
JavaScript type detection
There are several types of type detection
- typeof
- instanceof
- Object.prototype.toString
- constructor
- duck type
The most commontypeof
It returns a string, perfect for function objects and primitive types
typeof 100= = = "number"typeof true= = = "Boolean"typeof function () {} = = ="function"typeof(undefined)) = = ="undefined"typeof(new Object()) = = ="object"typeof( [1.2] ) = = ="object"typeof(NaN ) = = ="number"typeof(null) = = ="object"Copy the code
typeof
It’s very handy to judge some basic types, function objects. But there is no way to determine the type of other objects. For example, if you want to determine whether an object is an array, if you usetypeof
Returns theobject
Obviously not what I wanted.
This is used to determine the type of an objectobj instanceof Object
[1.2] instanceof Array= = =true
new Object(a)instanceof Array= = =false
function person(){};
function student(){};
student.prototype=new person();
var bosn =new student();
console.log(bosn instanceof student)//true
var one =new person();
console.log(one instanceof person)//true
console.log(bosn instanceof person)//true
Copy the code
Object.prototype.toString
* * 6/7/8 Object. The prototype. ToString. Apply (null) returns “[Object Object]” * *
Object.prototype.toString.apply([]); = = = "[objectArray] ";Object.prototype.toString.apply(function(){}); = = = "[objectFunction] ";Object.prototype.toString.apply(null); = = = "/ object Null"Object.prototype.toString.apply(undefined); = = = "[object Undefined]"Copy the code
Typeof is suitable for basic type and function detection, null invalidation.
[[Class]] via {}. ToString, suitable for built-in objects and primitive types, null and undefined (IE678 etc return [object object]).
Instanceof is suitable for custom objects and can also be used to detect native objects, failing to detect between different IFrames and Windows.