简体   繁体   English

&& 和 || 有什么区别在 JavaScript 术语中?

[英]What is the difference between && and || in JavaScript term?

My classmate used "||"我同学用了“||” instead of "&&" for an if statement, but the result is the same.代替 if 语句的“&&”,但结果是相同的。 I tried to understand the differences by reading the article but I can't seem to understand them.我试图通过阅读文章来理解差异,但我似乎无法理解它们。 I would appreciate if anyone can explain in an easy way...Thank you!如果有人能以简单的方式解释,我将不胜感激……谢谢!

&& is and operator and || && 是 and 运算符 and || is or operator && work as是或运算符 && 工作为

1 && 1= 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0

|| || work as作为

1 || 1= 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0

where 1 , 0 are true and false we can understand it by the following example其中 1 , 0 是真假我们可以通过下面的例子来理解

suppose we have a code假设我们有一个代码

if(1==1 && 2==2){}

so 1==1 is true means 1 and 2==2 is true means 1, so && operator work like this 1 && 1 =1 hence the result will be true and execute if statement所以 1==1 为真意味着 1 和 2==2 为真意味着 1,所以 && 运算符像这样 1 && 1 =1 因此结果将为真并执行 if 语句

You could replace logical OR ||您可以替换逻辑 OR || with logical AND && and vice versa, by using De Morgan's laws :逻辑 AND &&反之亦然,通过使用德摩根定律

!(a && b) = !a || !b 
!(a || b) = !a && !b

If you have如果你有

if (a || b)

you could use this without OR你可以在没有 OR 的情况下使用它

if (!(!a && !b))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM