如题所述
判断一个值是否未定义,就是判断值是否undefined
可以通过typeof()这个方法来获取值的类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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为数组
}
可以通过typeof()这个方法来获取值的类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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为数组
}
温馨提示:答案为网友推荐,仅供参考