众所周知,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. 第06组 Beta版本演示

    队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/12052263.html 作业博客 : https://edu.cnblogs.com/campus ...

  2. zookeeper安装运行(docker)

    拉取镜像docker pull zookeeper:latest 获取镜像基本信息docker inspect zookeeper mkdir /opt/zookeeper -p vim /opt/z ...

  3. [代码质量] Git统计本次提交新增代码行数,建议每个评审commit新增行数小于400行

    git log HEAD~1..HEAD --author="$(git config --get user.name)" --pretty=tformat: --numstat ...

  4. (二)OpenCV-Python学习—对比度增强

    ·对于部分图像,会出现整体较暗或较亮的情况,这是由于图片的灰度值范围较小,即对比度低.实际应用中,通过绘制图片的灰度直方图,可以很明显的判断图片的灰度值分布,区分其对比度高低.对于对比度较低的图片,可 ...

  5. Nginx搭建简单文件下载服务器

    在C:\pleiades\nginx-1.16.1下新建一个目录files,然后放入若干文件,接下来修改nginx.conf,增加粗体字如下: #user nobody; worker_process ...

  6. WAL streaming (max_wal_senders > 0) requires wal_level "replica" or "logical"

    初次使用pg的11版本,执行以下操作修改归wal_level设置: alter system set set wal_level='minimal'; 尝试重启pg,发现重启失败,并报错: waiti ...

  7. idea搭建简单ssm框架的最详细教程(新)

    为开发一个测试程序,特搭建一个简单的ssm框架,因为网上看到很多都是比较老旧的教程,很多包都不能用了,eclipes搭建并且其中还附带了很多的其他东西,所以特此记录一下mac中idea搭建过程. 另: ...

  8. mysql-connector-java(6.0以上)的时差问题

    一.背景 通过mybatis日志观察插入数据库的时间为当前时间,但是打开数据库表发现时间滞后了8个小时. 二.推论及解决 很容易猜到这是时区的问题. 三.最后找到的问题点如下: jdbc:mysql: ...

  9. 从Linux服务器下载文件夹到本地

    从Linux服务器下载文件夹到本地 1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文 ...

  10. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...