愿所有的美好和期待都能如约而至

为什么在JS中将NULL与逻辑运算符一起使用会引发错误

发布时间:  来源:互联网  作者:匿名  标签:error why using NULL with logical operator throws error in JS exception IT资讯 jav  热度:37.5℃

本文介绍了为什么在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与逻辑运算符一起使用会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

勇敢去编程!

勇敢的热爱编程,未来的你一定会大放异彩,未来的生活一定会因编程更好!

TOP