JavaScript 数据类型梳理

编辑时间:2019-02


JavaScript 的数据类型有 7 种

  1 Undefined
  2 Null
  3 Boolean
  4 String
  5 Number
  6 Symbol ( ES6 新增 )
  7 Object


typeof 的返回值有 7 种

  1 "undefined"
  2 "boolean"
  3 "string"
  4 "number"
  5 "symbol" ( ES6 新增 )
  6 "object"
  7 "function"


常见数据的类型对照表

valuetypeof 结果对应类型

var a;

window.undefined;

typeof a == “undefined”;

typeof window.undefined == “undefined”;

typeof x == “undefined”;

Undefined
var b = false;typeof b == “boolean”;Boolean
var s = “”;typeof s == “string”;String
var n = 1;typeof n == “number”;Number
var s = Symbol(“s”);typeof s == “symbol”;Symbol

var o = {};

var nu = null;

typeof o == “object”;

typeof nu == “object”;

Object

Null

var f = function(){};typeof f == “function”--


具体类型判断

  1 Object.prototype.toString.call(2) // "[object Number]"
  2 Object.prototype.toString.call('') // "[object String]"
  3 Object.prototype.toString.call(true) // "[object Boolean]"
  4 Object.prototype.toString.call(undefined) // "[object Undefined]"
  5 Object.prototype.toString.call(null) // "[object Null]"
  6 Object.prototype.toString.call(Math) // "[object Math]"
  7 Object.prototype.toString.call({}) // "[object Object]"
  8 Object.prototype.toString.call([]) // "[object Array]"
  9 Object.prototype.toString.call(function(){}) // "[object Function]"
 10 // 参考:http://javascript.ruanyifeng.com/stdlib/object.html#toc8
posted @ 2019-02-28 19:54  hgy9473  阅读(87)  评论(0)    收藏  举报