为什么2&& 3(javascript)中有3个结果? [英] Why does 2 && 3 results in 3 (javascript)?
本文介绍了为什么2&& 3(javascript)中有3个结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在浏览器的控制台中键入时:
When I type in browser's console:
console.log(2 && 3)
结果总是带有第二个数字(在这种情况下为3):
it results always with second number (in this case 3):
3
有人可以解释为什么吗?
Can someone explain me why?
推荐答案
如果&&
的左侧为假值,则整个表达式的值为左侧.
If the left hand side of &&
evaluates as a false value, the whole expression evaluates as the left hand side.
否则,它的评估结果为右侧.
Otherwise it evaluates as the right hand side.
2
是一个真值,所以2 && 3
是3
.
2
is a true value, so 2 && 3
is 3
.
为进行比较,请尝试console.log(0 && 1)
和console.log(false && "something")
.
For comparison, try console.log(0 && 1)
and console.log(false && "something")
.
这篇关于为什么2&& 3(javascript)中有3个结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文