美文网首页
js 数据类型,以及如何判断数据类型

js 数据类型,以及如何判断数据类型

作者: 一方天地sq | 来源:发表于2018-07-14 22:08 被阅读0次

js中共存在7中数据类型 string,number,boolean,object,arrya,null,undefined

  1. 使用typeof操作符。
      对一个值使用 typeof 操作符可能返回下列某个字符串,返回的类型都是字符串形式。
      (1) undefined:如果这个值未定义
      (2) boolean:如果这个值是布尔值
      (3) string:如果这个值是字符串
      (4) number:如果这个值是数值
      (5) object:如果这个值是对象或null
      (6) function:如果这个值是函数
      需要注意:typeof不适合用于判断是否为数组。当使用typeof判断数组和对象的时候,都会返回object。
           可以使用isArray()来判断是否为数组。
    判断数据类型可以通过使用Object.prototype.toString方法
    console.log(Object.prototype.toString.call(“字符串”) === ‘[object String]’) -------> true; console.log(Object.prototype.toString.call(123) === ‘[object Number]’) -------> true; console.log(Object.prototype.toString.call([1,2,3]) === ‘[object Array]’) -------> true; console.log(Object.prototype.toString.call(new Date()) === ‘[object Date]’) -------> true; console.log(Object.prototype.toString.call(function a(){}) === ‘[object Function]’) -------> true; console.log(Object.prototype.toString.call({}) === ‘[object Object]’) -------> true;

相关文章

网友评论

      本文标题:js 数据类型,以及如何判断数据类型

      本文链接:https://www.haomeiwen.com/subject/lihupftx.html