日期:2014-05-16 浏览次数:20416 次
下面是typeof运算符对各个类型的返回结果: 1.undefined:undefined 2.null:object 3.string:string 4.number:number 5.boolean:boolean 6.function:function 7.object:object
<script type="text/javascript"> <!-- alert(typeof(NaN));//输出number var a=undefined;//等价于var a; alert(typeof (a));//输出undefined alert(null == undefined);//输出true alert(null==null);//输出true alert(NaN == NaN);//输出false alert(typeof (m));//输出undefined,一个未定义的对象 alert(typeof(document.xx));//输出undefined,一个不存在的对象属性 //--> </script>
<script> <!-- var t1 = null; var t2;//或var t2 = undefined; if(t1 === undefined){ alert('t1 undefined!'); } if(t2 === undefined){ alert('t2 undefined!'); } if(typeof(t2) == 'undefined'){ alert('t2 undefined use typeof!'); } //同时判断是否为undefined和null的方法,如下: if(!t1){ alert('t1 is not set value!'); } if(!t2){ alert('t2 is not set value!'); } //--> </script>
<script> <!-- function f1(){ document.write('test function <br/>'); } function f2(){ document.write('test function2 <br/>'); return; } alert(f1());//等价于:alert(typeof f1());输出undefined alert(f2());//等价于:alert(typeof f2());输出undefined //--> </script>