JS 中如何判断 undefined null

JavaScript 中有两个特殊数据类型:undefined 和 null,在写代码时该如何判断。

1、js中判断undefined:

不正确的写法;


var exp = undefined;
if (exp == undefined)
{
    alert("undefined");
}

exp 为 null 时,也会得到与 undefined 相同的结果,虽然 null 和 undefined 不一样。注意:要同时判断 undefined 和 null 时可使用本法。

正确写法:

var exp = undefined;
if (typeof(exp) == "undefined")
{
    alert("undefined");
}

 2、js判断null:

不正确写法:


var exp = null; 
if (exp == null) 
{ 
alert(“is null”); 
}
 
exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。注意:要同时判断 null 和 undefined 时可使用本法。
 
var exp = null; 
if (!exp) 
{ 
alert(“is null”); 
}
 
如果 exp 为 undefined 或者数字零,也会得到与 null 相同的结果,虽然 null 和二者不一样。注意:要同时判断 null、undefined 和数字零时可使用本法。
 
var exp = null; 
if (typeof(exp) == “null”) 
{ 
alert(“is null”); 
}
 
为了向下兼容,exp 为 null 时,typeof 总返回 object。
 
var exp = null; 
if (isNull(exp)) 
{ 
alert(“is null”); 
}
 
JavaScript 中没有 isNull 这个函数。

正确写法:

var exp = null; 
if (!exp && typeof(exp)!=”undefined” && exp!=0) 
{ 
alert(“is null”); 
} 

 更简单写法;

var exp = null;
if (exp === null){
    alert("is null");
}

尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用等于运算符(==)或严格等于运算符(===)来判断。 举个例子: ``` let x; console.log(x == undefined); // true console.log(x === undefined); // true console.log(x == null); // true console.log(x === null); // false ``` 另外,也可以使用 typeof 运算符来判断: ``` let x; console.log(typeof x === "undefined"); // true ``` 但是需要注意的是,使用 typeof 运算符判断 null 时会返回 "object",所以不能使用 typeof 运算符判断 null。 ### 回答2: 在JavaScriptundefinednull都代表着“不存在”的状态。虽然它们非常相似,但使用方法略有不同。 1. undefined undefined代表着声明了一个变量,但未给它赋值,或者访问一个不存在的对象属性或方法时返回的值。判断一个变量是否为undefined,可以通过typeof运算符来实现,如下所示: ``` let a; console.log(typeof a); // "undefined" ``` 另外,可以和undefined进行比较来判断一个变量是否为undefined,如下所示: ``` let a; if (a === undefined) { console.log("a is undefined."); } ``` 需要注意的是,尽管undefined是一个全局对象的属性,但不应该直接把它作为变量名来使用。 2. null null代表着一个空对象指针。通常用于初始化一个变量或对象时,将其设置为null判断一个变量是否为null,可以直接使用相等运算符“==”或“===”,如下所示: ``` let b = null; if (b == null) { console.log("b is null."); } if (b === null) { console.log("b is null and its type is null."); } ``` 需要注意的是,null并不等同于undefined或false,并且typeof null的返回值为"object"。 综上所述,判断undefinednull可以使用typeof运算符、相等运算符“==”或“===”,需要根据实际场景选择合适的判断方式。同时,在开发应注意避免直接使用undefinednull作为变量名来使用。 ### 回答3: 在JavaScript,我们可以使用typeof操作符来判断一个变量的值是否为undefinednull。 1. 判断undefined 如果变量未定义或已定义但未赋值,它的值就是undefined。我们可以使用typeof来检测变量是否为undefined。 语法:typeof 变量名 === "undefined" 示例: ``` var a; // a未定义 console.log(typeof a === "undefined"); // 输出true ``` 2. 判断null null是一个表示“没有对象”的特殊值,表示该变量值为空值。我们可以使用类型判断或“==”运算符来判断变量是否为null。 (1)使用typeof操作符 语法:typeof 变量名 === "object" && 变量名 === null 示例: ``` var a = null; console.log(typeof a === "object" && a === null); // 输出true ``` (2)使用“==”运算符 注意:当使用“==”运算符进行比较时,如果要判断的变量是undefined,则需要使用“===”运算符判断。 语法:变量名 == null 示例: ``` var a = null; console.log(a == null); // 输出true var b; // b未定义 console.log(b === undefined); // 输出true console.log(b == null); // 输出false ``` 综上所述,我们可以使用typeof操作符或“==”运算符来判断变量是否为undefinednull。但需要注意的是,使用“==”运算符时,如果要判断的变量是undefined,则需要使用“===”运算符判断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值