众所周知,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. flask 开发用户登录注册功能

    flask 开发用户登录注册功能 flask开发过程议案需要四个模块:html页面模板.form表单.db数据库操作.app视图函数 1.主程序 # app.py # Auther: hhh5460 ...

  2. JVM探究之 —— HotSpot虚拟机对象探秘

    本节以常用的虚拟机HotSpot和常用的内存区域Java堆为例,深入探讨HotSpot虚拟机在Java堆中对象分配.布局和访问的全过程. 1. 对象的创建 Java是一门面向对象的编程语言.在语言层面 ...

  3. Python3教程—很经典可以快速上手

    原文地址:https://www.runoob.com/python3/python3-tutorial.html Python 3 教程 Python 的 3.0 版本,常被称为 Python 30 ...

  4. 自定义Spring Boot内置tomcat的404页面

    spring boot 的相关404页面配置都是针对项目路径下的(如果配置了 context-path) 在context-path不为空的情况下,如果访问路径不带context-path,这时候会显 ...

  5. SQLServer ROW_NUMBER()函数使用方法 分区排序

    #ROW_NUMBER() over()能干什么? 既可满足分区的需求,也可以根据一定的顺序来排序. #细细说 select ROW_NUMBER() over(partition by xm Ord ...

  6. ArrayList: java之ArrayList详细介绍(转)

    1  ArrayList介绍 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List ...

  7. AIX 系统参数配置

    AIX 系统参数配置 原创 Linux操作系统 作者:fanhongjie 时间:2008-05-08 22:46:37 540 0 AIX内核属于动态内核,核心参数基本上可以自动调整,因此当系统安装 ...

  8. Flutter UI系统

    我们可以看到,无论是Android SDK还是iOS的UIKit 的职责都是相同的,它们只是语言载体和底层的系统不同而已.那么可不可以实现这么一个UI系统:可以使用同一种编程语言开发,然后针对不同操作 ...

  9. App installation failed (A valid provisioning profile for this executable was not found)

    真机调试build success ,App installation failed (A valid provisioning profile for this executable was not ...

  10. [IR] Inverted Index & Boolean retrieval

    教材:<信息检索导论> 倒排索引 How to build Inverted Index? 1. Token sequence. 2. Sort by terms. 3. Dictiona ...