首页

关于javascript的typeof运算符类型判断变量undefined和null区别及六种类型代码示例

标签:javascript,typeof运算符,undefined和null区别,六种类型     发布时间:2018-09-25   

一、前言

关于JavaScript使用typeof运算符,对undefined和null空值的代码测试,并对"number," "string," "boolean," "object," "function," 和 "undefined."六种数据类型进行测试。

二、代码示例

1. undefined和null

<script type="text/javascript"> @b@@b@	function funObj(){@b@		var a=null;@b@	}@b@	@b@	function main(){ @b@		var x; @b@		alert(x);@b@		if(typeof(x)=='undefined'){@b@			alert(funObj.a);@b@		}else{@b@			alert('a is not null!');@b@		}  @b@@b@	}@b@	@b@	main();@b@	@b@</script>

窗口打印结果

undefined@b@undefined

2. 六种数据类型

<script type="text/javascript"> @b@@b@	function funObj(){@b@	  var j=null;@b@	}@b@@b@	function main(){ @b@		var x=2;@b@		var y=0.2;@b@		var z='xyz';@b@		alert(typeof(x));@b@		alert(typeof(y));@b@		alert(typeof(z)); @b@@b@		alert(typeof(funObj));  @b@		return; @b@	}@b@	@b@	main();@b@</script>

窗口打印结果

number@b@number@b@string@b@function