javascript 怎么判断一个值是未定义的?

来源:百度知道 编辑:UC知道 时间:2024/06/25 08:06:42
我在一个数组里面放置了一些字符串,想把字符串变成代码去执行
var temp=eval(vars[i]);
那么在执行前和执行后像判断一下vars[i]和temp是否为空或者未定义怎么判断?

判断一个值是否未定义,就是判断值是否undefined

可以通过typeof()这个方法来获取值的类型

var a ;
if(typeof(a)==="undefined"){
//a为undefined类型
}

a = 123;
if(typeof(a)==="number"){
//a为number类型
}

a={};
if(typeof(a)==="object"){
//a为object类型
}

a="abc";
if(typeof(a)==="string"){
//a为string类型
}

a=true;
if(typeof(a)==="boolean"){
//a为boolean类型
}

a=function(){};
if(typeof(a)==="function"){
//a为function类型
}

a=[];
if(typeof(a)==="object"){
//值为数组的时候,typeof返回也是"object"
}

要判断值是否为数组,可以通过instanceof方法,判断一个值是否为另一个值的实例
a=[];
if(a instanceof Array){
//a为数组
}

var exeCode = vars[i];

if( typeof( exeCode ) !="undefined" || exeCode !=null )
{