用js如何判断数据类型
来源:互联网 发布:windows快捷键虚拟桌面 编辑:程序博客网 时间:2024/04/29 10:02
常见的数据类型有:
- 原始类型:
- Boolean - - 布尔值(返回值为false或true)
- Null
- Undefined- - 未定义变量
- Number- - 数字型
- String- - 字符串型
- Objec t- - 如果变量是一种引用类型或 Null 类型的
根据js的数据类型,举几个例子:
var a;var b=1;var c="1";var d=[1,2,3];var e=function(){};
方法一:typeof来检查数据类型
typeof a -->undefined;
typeof b -->number;
typeof c -->string;
typeof d -->object;
typeof e -->function;
通过返回的布尔值来判断数据类型
typeof a =="undefined"; //true
typeof b =="number"; //true
typeof c =="string"; //true
typeof d =="object"; //true
typeof e =="function"; //true
方法二:instanceof来判断数据类型
a instanceof Undefined; //true
b instanceof Number; //true
c instanceof String; //true
d instanceof Object; //true
e instanceof Function; //true
注意:instanceof后面一定要是对象类型,并且大小写不能错
a.constructor==Undefined; //true
b.constructor ==Number; //true
c.constructor ==String; //true
d.constructor==Array; //true
e.constructor ==Function; //true
方法四:prototype判断数据类型
Object.prototype.toString.call(a) === ‘[object Undefined]’); // true;
Object.prototype.toString.call(b) === ‘[object Number]’); // true;
Object.prototype.toString.call(d) === ‘[object Array]’); // true;
Object.prototype.toString.call(e) === ‘[object Function]’); // true;
- 用js如何判断数据类型
- js如何判断数据类型
- 如何判断js数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断js中的数据类型
- 如何判断 js 中的数据类型
- 如何判断js的数据类型
- js如何判断变量的数据类型?
- js如何判断数据类型和数组类型整理
- js中数据类型判断
- 在 Win7 使用 hyperledger fabric source code建构 Local Blockchain Network System
- Medium 209题 Minimum Size Subarray Sum
- 动画
- EntityFramework中的Database first简介(上)
- try catch 错误处理 也可以用于解决浏览器兼容性问题 Error对象
- 用js如何判断数据类型
- 他日一席必有我,blindblindSunShine
- 喝酒小游戏
- Android 笔记 popupMenu 显示图标
- Medium 43题 Multiply Strings
- Easy 235题 Lowest Common Ancestor of a Binary Search Tree
- mysql存储引擎性能比较
- C/C++编译预处理
- http协议返回状态代码及相应含义