为什么结果bool(true)&&字符串是javascript中的字符串? [英] Why the result of bool(true) && string is string in javascript?

查看:141
本文介绍了为什么结果bool(true)&&字符串是javascript中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试代码是:

console.log(true && "abc");//abc

谁能告诉我为什么结果是 abc

who can tell me why the result is abc?

推荐答案

来自 MDN(逻辑运算符) - 逻辑和(&&)


如果可以转换则返回expr1为假;否则,返回
expr2。因此,当与布尔值一起使用时,&&如果两个
个操作数都为真,则返回true;否则,返回false。

Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

true 显然无法评估为false,所以返回第二个值,即abc。

true obviously cannot be evaluated to false, so the second value is returned which is "abc".

这篇关于为什么结果bool(true)&&字符串是javascript中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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