Home  >  Article  >  Web Front-end  >  Share comparisons between JavaScript types

Share comparisons between JavaScript types

零下一度
零下一度Original
2017-06-26 13:26:571083browse

Types of JavaScript

Original type:

  • number

  • string

  • boolean

  • null

  • ##undefined

Object type:

  • Object

  • function

  • Array

  • Date

  • ...

Implicit conversion

+/- Operation

  • "37" + 7 = "377"

  • "37" - 7 = 30

== Operation

The following is true:

  • "1.23" == 1.23

  • 0 == false

  • null == undefined

Comparison operation

===Strictly equal to

  • Different types, return false

  • Same type, the following is true:

  • null === null

  • undefine === null

  • NaN != NaN

  • new Object != new Obejct

== is equal to

  • and has the same type as

    ===

  • Different types, try type conversion comparison

  • null == undefined

  • number == string to number

  • boolean == ? Convert number

  • Object == number | string Try to convert the object to a basic type

  • Others: false

Packaging type

In order to facilitate the operation of basic type values, Js provides the automatic packaging function of basic types, and each order reads a basic type value When called, the background will create an object of the corresponding basic packaging type and automatically destroy it after the call.

Since the meanings of basic packaging types and basic types are different, operations such as typeof will produce different results. It is not recommended to display instantiated basic data types

var a = "string";
alert(a.length);    //6

a.t = 3;
alert(a.t);         //undefined
Type detection

typeof

The following is true:

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”
instanceof

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.

The following is true:

[1, 2] instanceof Array === true
new Object() instanceof Array === false
Object.prototype.toString.apply()

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]”

Summary

  • typeof

    Suitable for basic type and function detection, and will fail when encountering null.

  • [[Class]]

    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] ).

  • instanceof

    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn