js判断是否为空和typeof的用法

(1)typeof作用
用于查看数据类型

(2)typeof用法
typeof 返回值类型有number, string, boolean, function, undefined, object
PS:在使用typeof()操作符时圆括号是可选项,可带可不带。即两种形式 typeof(XX) 或 typeof XX

1  console.log(typeof 2); // number
2  console.log(typeof "2"); // string
3  console.log(typeof true); // boolean
4  console.log(typeof [2]); // object
5  console.log(typeof {name:2});// object
6  console.log(typeof function(){return 2});// function
7  console.log(typeof new Date());// object
8  console.log(typeof null); // object
9  console.log(typeof undefined);// undefined

但typeof只能区分number, string, boolean, function及undefined,其他的对象、数组、日期、null等都返回Object,存在缺陷。
利用Object.prototype.toString.call可以很好的区分各种类型(注:无法区分自定义对象类型,自定义类型可以采用instanceof区分)

 1 console.log(Object.prototype.toString.call("zhangsan"));//[object String]
 2 console.log(Object.prototype.toString.call(12));//[object Number]
 3 console.log(Object.prototype.toString.call(true));//[object Boolean]
 4 console.log(Object.prototype.toString.call(undefined));//[object Undefined]
 5 console.log(Object.prototype.toString.call(null));//[object Null]
 6 console.log(Object.prototype.toString.call({name: "zhangsan"}));//[object Object]
 7 console.log(Object.prototype.toString.call(function(){}));//[object Function]
 8 console.log(Object.prototype.toString.call([]));//[object Array]
 9 console.log(Object.prototype.toString.call(new Date));//[object Date]
10 console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
11 function Person(){};
12 console.log(Object.prototype.toString.call(new Person));//[object Object]

(3)js判断是否为空

1 var exp = null;
2 if (!exp && typeof(exp)!="undefined" && exp!=0 && exp!=‘‘)
3 {
4   alert("is not null");
5 } 

原文地址:https://www.cnblogs.com/xmm2017/p/11470323.html

时间: 2024-08-28 15:15:58

js判断是否为空和typeof的用法的相关文章

js判断是否为空火undefined是否给undefined加引号

js判断是否为空为undefined如果判断的是类型则用typeof(),如果其本身就是“undefind”字符窜,则不能这样判断, 这样typeof之后是字符串类型就判断不出来了,这是需要去掉typeof直接判断两个字符串相等

js判断是否为空

http://dushanggaolou.iteye.com/blog/1293803 1.<input type="hidden" id="key" name="key" value="123"> 用js判断隐藏域的值是否为空, 方法一: Java代码   var keyVal= $("#key").val(); if(keyVal==undefined || keyVal=="&q

JS判断内容为空方法总结

HTML代码: 用户名:<input type="text" id="username"> <p style="color:red" id="warning-message"></p> JS代码: // 获取对象 var oUsername = document.getElementById('username'); var oMsg = document.getElementById('w

js 判断是否是空对象

主要思路 我们要考虑到的主要有:js原生对象,宿主对象(浏览器上面的). 首先对于宿主对象 主要判断是DOM 对象 和是否是window 对象 是否是DOM对象  value.nodeType 是否存在. 是否是window 对象,value != null  && value === value.window; 对于这两种肯定不是空对象. if(value.nodeType || isWindow(value)){     return false; } 对于js原生对象 是否本身返回就

js判断对象为空

http://www.jb51.net/article/42713.htm var isEmptyValue = function(value) { var type; if(value == null) { // 等同于 value === undefined || value === null return true; } type = Object.prototype.toString.call(value).slice(8, -1); switch(type) { case 'Strin

js判断字段是否为空 isNull

//在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true . //为空判断函数 function isNull(arg1) {  return !arg1 && arg1!==0 && typeof arg1!=="boolean"?true:false; } //alert(isNull(null));    //true //alert(isNull(''));    

js判断字符串是否全为空(使用trim函数/正则表达式)

我们需要判断用户输入的是否全是空格,可以使用以下方法: 方法一: 使用trim() /* 使用String.trim()函数,来判断字符串是否全为空*/ function kongge1(test) { let str = test.trim(); if (str.length == 0) { console.log('字符串全是空格'); } else { console.log('输入的字符串为:' + test); } } 如果 trim() 不存在,可以在所有代码前执行下面代码 /* 给

js判断登陆用户名及密码是否为空的简单实例

js判断登陆用户名及密码是否为空的简单实例 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <script type="text/javascript"> // 验证输入不为空的脚本代码 function checkForm(form) { if(form.username.value == "") { alert("用户名不能为空!"); form.username.focus(); return

js判断复合数据类型的两种方式(typeof不奏效了)

js判断复合数据类型的两种方式(typeof不奏效了) 博客分类: Web前端-JS语言核心 JavaScript 作者:zccst typeof认为所有的复合数据类型都是"object",没法进一步细分,所以还得用其他方法 先上结论: 1,(arr && typeof(arr) === "object" && arr.constructor === Array) 2,Object.prototype.toString.call(ar