1. 使用typeof操作符判断变量的类型是否为'undefined'。
if(typeof variable !== 'undefined') {
    // 变量不为undefined
}
  1. 使用全等操作符判断变量是否为undefined。
if(variable !== undefined) {
    // 变量不为undefined
}
  1. 使用全等操作符判断变量是否为null。
if(variable !== null) {
    // 变量不为null
}
  1. 使用!操作符判断变量是否为undefined或null。
if(variable != null) {
    // 变量不为undefined或null
}
  1. 使用!!操作符将变量转换为布尔类型,判断是否为真。
if(!!variable) {
    // 变量不为undefined
}
js判断不为undefined的 几种方法

原文地址: http://www.cveoy.top/t/topic/ib2r 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录