JS中的數(shù)據(jù)類型及判斷

基本數(shù)據(jù)類型

Number、String、null、undefined、boolean

引用數(shù)據(jù)類型

Object、function、Array、RegExp、new Date、 Math 實例對象。。。 function

es6新增數(shù)據(jù)類型

Synbol

其他

NaN

檢測數(shù)據(jù)類型

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 的引用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評論 0 2
  • 1.通過typeof可以判斷處幾種基本數(shù)據(jù)類型Boolean,number,string,null,undefin...
    舟漁行舟閱讀 690評論 0 1
  • 數(shù)據(jù)類型 基本類型:String、Number、Boolean、Undefined、Null 。由于其占據(jù)空間固定...
    小腳色閱讀 528評論 0 1
  • 第一章: JS簡介 從當(dāng)初簡單的語言,變成了現(xiàn)在能夠處理復(fù)雜計算和交互,擁有閉包、匿名函數(shù), 甚至元編程等...
    LaBaby_閱讀 1,761評論 0 6
  • 硅谷天使投資人納瓦爾·拉威康特(Naval Ravikant),曾投資過包括推特和優(yōu)步在內(nèi)的100多家創(chuàng)業(yè)公司。拉...
    赫薾拉娜閱讀 188評論 0 0

友情鏈接更多精彩內(nèi)容