JS中的数据类型及判断

基本数据类型

Number、String、null、undefined、boolean

引用数据类型

Object、function、Array、RegExp、new Date、 Math 实例对象。。。 function

es6新增数据类型

Synbol

其他

NaN

检测数据类型

var fn = funcion(){};
var obj = {};
var num = 1;

  1. typeof
    typeof NaN => "number"
    typeof null =>"object"
    typeof 2 => "number"
    typeof "aa" => "string"
    typeof fn => "function"
    typeof obj => "object"
  2. instanceof
    "aa" instanceof String => true
    fn instanceof Function => true
    obj instanceof Object => true
    1 instanceof Number => false
    number instanceof Number => false;
    NaN instanceof NaN => TypeError: Right-hand side of 'instanceof' is not an object
    false instanceof Boolean => false
    3.toString
    Object.prototype.toString.call('') ; // [object String]
    Object.prototype.toString.call(1) ; // [object Number]
    Object.prototype.toString.call(true) ; // [object Boolean]
    Object.prototype.toString.call(Symbol()); //[object Symbol]
    Object.prototype.toString.call(undefined) ; // [object Undefined]
    Object.prototype.toString.call(null) ; // [object Null]
    Object.prototype.toString.call(new Function()) ; // [object Function]
    Object.prototype.toString.call(new Date()) ; // [object Date]
    Object.prototype.toString.call([]) ; // [object Array]
    Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
    Object.prototype.toString.call(new Error()) ; // [object Error]
    Object.prototype.toString.call(document) ; // [object HTMLDocument]
    Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容