Original type:
number
string
boolean
null
Operation
Operation
Strictly equal to
is equal to
===
var a = "string"; alert(a.length); //6 a.t = 3; alert(a.t); //undefined
typeof 100 === “number” typeof true === “boolean” typeof function () {} === “function” typeof(undefined) ) === “undefined” typeof(new Object() ) === “object” typeof( [1, 2] ) === “object” typeof(NaN ) === “number” //NaN也为number typeof(null) === “object”
obj instanceof Object Use the prototype chain for judgment, suitable for judgment between objects. It expects an object on the left and a function object or function constructor on the right.
[1, 2] instanceof Array === true new Object() instanceof Array === false
Object.prototype.toString.apply([]); === “[object Array]”; Object.prototype.toString.apply(function(){}); === “[object Function]”; Object.prototype.toString.apply(null); === “[object Null]” Object.prototype.toString.apply(undefined); === “[object Undefined]” // IE6/7/8 Object.prototype.toString.apply(null) 返回”[object Object]”
Suitable for basic type and function detection, and will fail when encountering null.
Get it through {}.toString, suitable for built-in objects and primitive types. When encountering null and undefined, it will fail (IE678, etc. return [object Object] ).
is suitable for custom objects and can also be used to detect native objects. It will fail when detected between different iframes and windows.
The above is the detailed content of Share comparisons between JavaScript types. For more information, please follow other related articles on the PHP Chinese website!