Check if variable is undefined
This question already has an answer here:
未定义是js中的虚假...它看起来像bar.baz可能是你的罪魁祸首。
You can use typeof to check if a variable is undefined
. It always returns a string
.
if (typeof foo === 'undefined') {
console.log('foo is undefined');
}
var foo = ['one', 'two', 'three'];
if (typeof foo !== 'undefined') {
// access elements
console.log(foo[0] + ', ' + foo[1] + ', ' + foo[2]);
}
链接地址: http://www.djcxy.com/p/76352.html
下一篇: 检查变量是否未定义