为什么使用!==何时可以使用===?
我可以简单地将代码验证为IF及其他代码。与!==相比,===更易于使用,为什么使用不相等的操作员?
function equality( number ){
if ( number === 7 ){
return "it's equal"
} else { return "not equal"}
}
console.log(equality(7))
function nonEquality( number ){
if ( number !== 7 ){
return "it's not equal"
} else { return "it's equal"}
}
console.log(nonEquality(7));
I could simply vice versa the code to be executed by the if and else. And === is more easier to use compared to !== so why is the not equal operator used?
function equality( number ){
if ( number === 7 ){
return "it's equal"
} else { return "not equal"}
}
console.log(equality(7))
function nonEquality( number ){
if ( number !== 7 ){
return "it's not equal"
} else { return "it's equal"}
}
console.log(nonEquality(7));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用条件语句在大多数常见情况下转换操作员。在您的示例中,使用IF-ELSE将非平等运算符转换为平等运算符将很容易且可读。但是,如果这样的条件怎么办?
您是否要在此处应用转换逻辑以替换非平等运算符?您的转换越多,您的代码就越复杂且无法读取。
You cannot use conditional statements to convert the operators for most common cases. In your example, it would be easy and readable to convert the non-equal operator to equal operator by using if-else. But what if a condition like this?
Do you want to apply converting logic here to replace the non-equal operator? The more you convert, then more complicated and unreadable your code is.