代码改变世界

typeof记录

2012-08-20 10:16  littlelion  阅读(171)  评论(0编辑  收藏  举报

总结一下javascript中typeof的用法,方便以后查阅。。

w3School上面对typeof的定义如下:typeof 运算符有一个参数,即要检查的变量或值。那就是判断类型嘛~~

ECMAScript中五种原始类型:number,string,undefined,boolean,null,好下面来试一下。

 

document.write(typeof(25)+"<br/>");

document.write(typeof("lemon")+"<br/>");

document.write(typeof(undefined)+"<br/>");

document.write(typeof(null)+"<br/>");

document.write(typeof(true)+"<br/>");

 

运行结果如下:

这里面typeof(null)返回了object,可以理解为:null是对象的占位符(有点怪怪的感觉)。

undefined & null的说明:

undefined是声明了但未被赋值的变量.......

null就是不存在的对象...