1. typeof 运算符

  typeof 可以判断基本数据类型:

  typeof 123; // "number"

  typeof 'abc'; // "string"

  typeof true; // "boolean"

  碰到复合数据类型的情况:

  typeof {}; // "object"

  typeof []; // "object"

  var f = function(){};

  typeof f; //'function'

  

  特殊情况:

  1.typeof null; // "object" 这是历史遗留问题,不做深究,是个隐藏的坑

  2. typeof undefined; (undefined指未定义的变量)// “undefined”

  利用这个特点 用于判断语句

  1. if (v) {
  2. // ...
  3. }
  4. // ReferenceError: v is not defined
  5.  
  6. // 正确的写法
  7. if (typeof v === "undefined") {
  8. // ...
  9. }

综上,利用typeof 得到值就有"number","string","boolean","object","function","undefined" 这6个值

2. instanceof 运算符

  对于对象和数组这种数据,利用typeof不能区分,这时候需要用到instanceof 运算符

  1. var o = {};
  2. var a = [];
  3.  
  4. o instanceof Array // false
  5. a instanceof Array // true

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

  1. js数据类型的判断方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  2. JS数据类型的判断

    在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示:    基本类型:String.Number.Boolean.Symbol.Undefine ...

  3. js数据类型及判断数据类型

    众所周知,js有7种数据类型 1. null 2. undefined 3. boolean 4. number 5. string 6. 引用类型(object.array.function) 7. ...

  4. 前端基础——js数据类型及判断方法

    一.数据类型 我们通常熟知的数据类型有六种,包括5种基本数据类型(Number, String, Boolean, Undefined, Null)和一种引用数据类型(Object).ES6又新增了一 ...

  5. js数据类型之判断

    js有几种类型,具体是:字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未定义(Undefined). js提供了typ ...

  6. 实现 js 数据类型的判断函数type

    type = (obj) => { const pass1 = typeof obj if (pass1 != 'object') return pass1 const pass2 = obj ...

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

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

  8. 正确判断js数据类型 总结记录

    正确判断js数据类型 总结记录 判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. 三方库. js六大数据类型 number: 数 ...

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

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

随机推荐

  1. 连续子数组的最大和 java实现

    package findMax; /** * 连续子数组的最大和 * @author root * */ public class FindMax { static int[] data = {1,- ...

  2. Struts 简单UI标签

    <!-- 服务器标签 : 最终别解析为html标签--> <s:form action="/user_login" method="post" ...

  3. 六十六:CSRF攻击与防御之CSRF防御之ajax防御和ajax封装

    app里面还是要绑定CSRFProtect from flask_wtf import CSRFProtect # flask_wtf 已经提供CSRF的防御手段CSRFProtect(app) # ...

  4. ANSI C遍历二维数组指针地址

    #include <stdio.h> int main() { ][] = {,,,}; //等价于{{1,2},{3,4}}; ; i < ; i++) { ; j < ; ...

  5. 抓包工具chlers的使用

    1,下载chlers破解版:https://zhubangbang.com/charles-crack-version-free-download-and-install-tutorial.html ...

  6. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

  7. collections(python常用内建模块)

    文章来源:https://www.liaoxuefeng.com/wiki/897692888725344/973805065315456 collections collections是Python ...

  8. CentOS7使用阿里云源安装Docker

    安装步骤 1.删除已安装的Docker # Uninstall installed docker sudo yum remove docker \ docker-client \ docker-cli ...

  9. ORACLE 正则匹配

    1.正则匹配 select CONCAT(TO_NUMBER(REGEXP_REPLACE('019年','[^0-9]')),'年') from dual;

  10. 【Linux开发】linux设备驱动归纳总结(七):2.内核定时器

    linux设备驱动归纳总结(七):2.内核定时器 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...