众所周知,js有7种数据类型

  1. null

  2. undefined

  3. boolean

  4. number

  5. string  

  6. 引用类型(object、array、function)

  7. symbol

判断类型有以下4种判读方法

第一种方式: typeof

  typeof null ---> "object"

  typeof undefined ---> "undefined"

  typeof true | false ---> 'boolean'

  typeof 42 ---> 'number'

  typeof "42" ---> 'string'

  typeof { name : '1'} | [] ---> 'object'

  typeof Symbol ---> 'symbol'

  typeof ()=>{} ---> 'function'

  typeif void 0 ---> 'undefined'

第二种方式 instanceof 但是这种方式只适合判断object类型

  比如 : var arr = [] ; arr instanceof Array ---> true

      null instanceof Object ---> false

     [function] instanceof Object | Function --> true

第三种方式 Object.prototype.toString.call() 这种方式可以将全部的数据类型检测出来 也是 推荐的方式

  因为toString是Object的原型方法, 而 Array Function 等都是Object的实例。都重写了toString 方法。返回的是类型的字符串

  Object.prototype.toString.call(null) ---> [object Null]

  Object.prototupe.toString.call(undefined) ---> [object Undefined]

  Object.prototype.toString.call(123) ---> [object Number]

  Object.prototype.toString.call(true) ---> [object Boolean]

  Object.prototype.toString.call('123') ---> [object String]

  Object.prototype.toString.call({}) ---> [object Object]

  Object.prototype.toString.call([]) ---> [object Array]

  Object.prototype.toString.call(Math) ---> [object Math]

  Object.prototype.toString.call(function(){}) ---> [object Function]

  Objdec.prototype.toString.call(new Date) ---> [object Date]

  Object.prototype.toString.call(Symbol()) ---> [object Symbol]

第四种方式: constructor 判断对象的构造函。

  1. null 是js 原型链的起点,没有构造函数

  2. undefined 没有构造函数

  3. [].constructor === Array ---> true

  4. [string].constructor === String

  5. [object].constructor === object

  6. [number].constructor === Number

  7. [symbol].constructor === Symbol

  8. [function].constructor === Function

  9. [new Date].constructor === Date

  10. [RegExp].constructor === RegExp

来源 https://www.cnblogs.com/amiezhang/p/10325558.html

js数据类型及判断数据类型的更多相关文章

  1. js中的数据类型和判断数据类型

    js中的数据类型和判断数据类型 基本数据类型,六大基本数据类型:字符串(String).数字(Number).布尔(Boolean).对象(Object).空(Null).未定义(Undefined) ...

  2. JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()

    1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...

  3. 浏览器解析js和type判断数据类型

    ### 浏览器解析: - 1.当浏览器(内核.引擎)解析和渲染js的时候,会给js提供一个运行的环境,这个环境叫做“全局作用域(后端global / 客服端window scope)” - 2.代码自 ...

  4. JS中的数据类型及判断数据类型的方法

    简单类型(基本类型): number,string,boolean,null,undefined 复杂类型(引用类型):object typeof 只能判断基本数据类型 instanceof 能够判断 ...

  5. js中准确判断数据类型的方法

    一   通用的typeof 方法 typeof  ture    输出   Boolean typeof  123   输出     number ..... 但是   typeof 无法判断  nu ...

  6. js判断数据类型方法

    //一般js中我们判断数据类型 都使用typeof 这里采用 Object.prototype.toString function type (val) { return Object.prototy ...

  7. js中判断数据类型的4中方法

    注意: js中数据类型有7种(number, boolean, string, null, undefined, object, Symbol(es6新增)) 原始数据类型: number, stri ...

  8. js判断数据类型的四种方法

    1.typeof typeof是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型.返回的结果用该类型的字符串(全小写字母)形式表示,包括number,string,boolean,und ...

  9. JS的数据类型(包含:7种数据类型的介绍、数据类型的转换、数据类型的判断)

    前言 最新的 ECMAScript 标准定义了JS的 7 种数据类型,其中包括: 6 种基本类型:Boolean.Null.Undefined.Number.String.Symbol (ECMASc ...

随机推荐

  1. @PathVariable不起作用,报错:Cannot resolve @PathVariable ' '

    @PathVariable是占位符注解.可以把Url中变量的值传递到方法参数中. 示例如下: @PostMapping("/user/name/{id}") @ResponseBo ...

  2. [Beta]第九次 Scrum Meeting

    [Beta]第九次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/19 21:20 20min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 (一人回 ...

  3. 并发下sftp连接报错——com.jcraft.jsch.JSchException: connection is closed by foreign host

    当对单接口极限测试时,随着并发量上升,接口稳定性出现不稳定的情况,排查后台日志,发现报错在该接口调用sftp上传时出现问题(确切的是在初始化连接时失败) 原因:系统SSH终端连接数配置过小,查看虚拟机 ...

  4. semi-join子查询优化 -- FirstMatch策略

    FirstMatch执行semi-join子查询的一种策略. 类似于MySQL 5.x中如何执行in.exists子查询. 让我们以搜索拥有大城市的国家为例: select * from Countr ...

  5. thinkphp3.2源码(错误和异常处理)

    写在前面:tp3.2中每次载入入口文件时都会进行错误和异常的捕获,解读这一部分代码可以对以后的优化很有好处.   处理概览:          错误捕获与处理: 致命错误捕获: 我们尝试在 Home/ ...

  6. npm install Error: EACCES: permission denied, mkdir

    今天研究Electron的时候,全局安装运行 npm install electron -g时侯,报下面的错误: Error: EACCES: permission denied, mkdir '/U ...

  7. [转]nodejs导出word

    转载自:https://blog.51cto.com/13803916/2133602 需要先下载依赖: npm install officegen 亲测可用: var officegen = req ...

  8. [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  9. Windows 7安装Service Pack 1失败问题

    问题 很多朋友在电脑上安装某些软件时,会发现安装不了,提示信息大多是"本软件只支持 Windows 7 SP1 及更新版本"等等.这说明,你的电脑仍然在运行最早的Windows7版 ...

  10. 从一个案例窥探ORACLE的PASSWORD_VERSIONS

    1.环境说明 ORACLE 客户端版本 11.2.0.1 ORACLE 服务端版本 12.2.0.1 2.异常现象 客户端(下文也称为Cp)访问服务端(Sp),报了一个错误: Figure 1 以错误 ...