js数据类型

来源:互联网 发布:兄弟连细说linux.pdf 编辑:程序博客网 时间:2024/05/15 00:57

js数据类型

简单数据类型:number、string、boolean、null(空对象的指针)、undefined(已声明未初始化)
复杂数据类型:object

数据类型判断

  • typeof
    typeof返回值只有undefined、boolean、number、string、object、function这几个值中的一个,返回值是字符串

typeof 23 ⇒ “number”
typeof ‘aaaa’ ⇒ “string”
typeof true ⇒ “boolean”
typeof null ⇒ “object”
typeof [1,2] ⇒ “object”
typeof {a:2, b:3} ⇒ “object”
typeof undefined ⇒ “undefined”
typeof error(错误)⇒ “undefined”

undefined值是派生自null,所以null==undefined ⇒ true

  • instanceof

    object instanceof constructor
    constructor包含Object/Function/Date/Array等等

    eg:

var m = {a:2, b:3};m instanceof Objecttruem instanceof Functionfalse
0 0
原创粉丝点击