为什么在JS中将NULL与逻辑运算符一起使用会引发错误 [英] why using NULL with logical operator throws error in JS

查看:0
本文介绍了为什么在JS中将NULL与逻辑运算符一起使用会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在测试的代码--

工作正常

document.write( 1 && undefined ); // prints undefined
document.write( 1 && 3 ); // prints 3 
document.write( 1 && true ); // prints  true

引发错误

document.write( 1 && NULL ); // throws Error 

为什么使用空抛出错误,尽管它即使对未定义的也有效

虽然我测试了NULL和它的类型undefined,但它仍然不起作用。让我知道这一点。(OOP编程新手)

推荐答案

NULL不存在,请尝试

try {
    document.write( 1 &&  NULL  );
} catch ( e) {
    document.write( 1 &&  null  );
}

这篇关于为什么在JS中将NULL与逻辑运算符一起使用会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆