js 数据类型的判断
2024-10-07 00:15:00
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”
利用这个特点 用于判断语句
if (v) {
// ...
}
// ReferenceError: v is not defined // 正确的写法
if (typeof v === "undefined") {
// ...
}
综上,利用typeof 得到值就有"number","string","boolean","object","function","undefined" 这6个值
2. instanceof 运算符
对于对象和数组这种数据,利用typeof不能区分,这时候需要用到instanceof 运算符
var o = {};
var a = []; o instanceof Array // false
a instanceof Array // true
最新文章
- List转MVC DropDownListFor(SelectList)
- ASP.NET文件上传大小的限制解决方案
- Java throw:异常的抛出怎么回事
- C++中string 的使用
- 第三篇、C_双向链表(循环链表)
- uvalive 5760 Alice and Bob (组合游戏,dp)
- 《APUE》-第五章标准IO库
- 菜鸟的jQuery源码学习笔记(前言)
- CSS排序工具csscomb
- java 制作QQ登录界面
- List集合就这么简单【源码剖析】
- 【IOS 开发】Object-C 入门 Xcode 环境详解
- 前端——JavaScript之if语句
- Linux命令:popd
- MySQL root密码忘记,原来还有更优雅的解法!
- Scala进阶之路-idea下进行spark编程
- LeetCode题解之Convert Sorted List to Binary Search Tree
- 【chrome】安装证书并配置为受信任网站连接(windows)
- 大纲2.3 Internet
- elk +redis 环境搭建
热门文章
- 转贴 使用正则表达式解析一般sql语句(C++)
- 《视觉SLAM十四讲》第1讲
- Hibernate3核心API-SchemaExport类
- 微信小程序开发工具下载以及安装教程
- redis(2)事务的订阅与发布
- Python——PYQT:控件基本使用
- Spring RedisTemplate常用方法(List,Hash)
- jdk1.8-stack 栈源码分析
- Go语言集成开发工具JetBrains GoLandMac2.3中文版
- 模块的概念、模块的导入方式【IMPORT 模块名、FROM 模块 IMOPRT 功能】、模块的搜索路径、链式导入&;循环导入