JavaScript合并&&;变量中的语句为true或false

JavaScript合并&&;变量中的语句为true或false,javascript,variables,Javascript,Variables,只是一个简单的问题。我找不到任何与此相关的东西,因为我真的不知道如何解释它。。。但是,如果我使用&&组合两个布尔值来生成另一个变量,会发生什么 var is_enabled = isEnabled() && isSupported(); 如果isEnabled()为false,isSupported()为true,它是否等于false?您可以做的是只添加一个&并对这些布尔值应用和操作,如果这两个布尔值都为true,那么将为true var is_enabled = isEnab

只是一个简单的问题。我找不到任何与此相关的东西,因为我真的不知道如何解释它。。。但是,如果我使用&&组合两个布尔值来生成另一个变量,会发生什么

var is_enabled = isEnabled() && isSupported();

如果isEnabled()为false,isSupported()为true,它是否等于false?

您可以做的是只添加一个
&
并对这些布尔值应用
操作,如果这两个布尔值都为true,那么
将为true

var is_enabled = isEnabled() & isSupported();
编辑
感谢您指出我的语法不正确,这应该适用于C语言,我想我只是被弄糊涂了,
false&&true
的结果是
false
如果isEnabled()为false,您使用&&then Isupported()将永远不会被调用,因为求值将短路

如果
和&
运算符的任何操作数是falsy(
false
,0,
null
未定义的
NaN
启用的
将被分配第一个falsy值


如果
&&
运算符的所有操作数都不为falsy,则最后一个操作数将分配给
已启用

仅当isEnabled和isSupported均为true时,isEnabled才会设置为true。因此,如果isEnabled为false,isSupported为true,则is_enabled将为false。

是:

<script type="text/javascript">
function isEnabled() {
    return false;
}

function isSupported() {
    return true;
}

var is_enabled = isEnabled() && isSupported();

alert(is_enabled);  // = 'false'
</script>

函数isEnabled(){
返回false;
}
函数isSupported(){
返回true;
}
var is_enabled=isEnabled()&&isupported();
警报(已启用);//='假'

如果两个函数都只返回true或false,那么它将与布尔函数一样正常工作

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

成功的布尔运算(如“&&”)的结果是一个布尔值。因此,
isEnabled()&&isSupported()
的结果将是一个布尔值,然后首先将该布尔值分配给is_enabled

,&&仅当且仅当两个表达式均为真时才为真

那么回到你的问题,true&&false将等于false,所以是的


您也可以尝试使用firebug或chrome开发者工具上的console函数自己测试这些表达式。

在Javascript中,
&&
|
操作符有点奇怪。这取决于该值是“falsy”(零、
未定义、
空、
空、
空字符串、
NaN
)还是truthy(任何其他值,包括空数组)

使用
&&
时,如果第一个值为“falsy”,则运算结果将为第一个值,否则将为第二个值。使用
|
时,如果第一个值是“falsy”,则运算结果将是第二个值,否则将是第一个值

例如:

var a = 5 && 3; // a will be 3
var a = 0 && 7; // a will be 0

var a = 1 || 2; // a will be 1
var a = 0 || 2; // a will be 2
如果要替换此选项,此选项非常有用:

if (x == null){
  x = 5;
}
与:

因此,简而言之,如果
isEnabled()
是真的,那么
是启用的
将被设置为
isSupported()
返回的任何值。如果
isEnabled()
为falsy,则
已启用
将设置为该falsy值的任何值

正如Robert指出的,还有短路:

var x = 5 || infinite_loop();
var x = false && infinite_loop();

在这两种情况下,
infinite_loop()
调用都不会发生,因为这两个操作是短路的-
|
在第一个值为truthy时不会计算第二个值,当第一个值为falsy时,
&&
不会计算第二个值。

这里您可以看到可能的测试情况:

var str='My dummy string';
var str2='My other dummy string';
var falsy1= 1==2;
var truthy1= true;
var truthy2= true;
var falsy2= false;
然后:


console.log('two-bool-true:',truthy1和truthy2);// 简短回答:
如果第一个不是虚假的那么第二个,
要不然先

示例:如果isEnabled()返回false,则结果为
false

否则,如果isEnabled()为true, 然后,无论isSupported()返回什么结果

现在使用
false
true
来简化答案,false可以是任何falsy

真实和虚假价值的例子:


var string=“”;//你试过了吗?它输出了什么?如果您的函数真的返回布尔常量
true
false
,那么是。Q:如果isEnabled()为false,isSupported()为true,它是否等于false?你在开玩笑吧?问:你试过了吗?问:你能想出任何可能的原因,它不会是假的吗???我不能尝试,因为我在移动atm上,但这个问题出现在我的脑海中。基本上,我的问题是,我是否可以将这两个函数与一个&&无错误组合起来。&&
的可能重复也是一个
操作
&
是一个按位的
。应该注意的是,
&
的语义与
&
有很大的不同,特别是
&
总是计算两个表达式,而
&
仅在左侧为
true时计算右侧(必须检查规格以确保:-)按位逻辑运算符总是返回一个整数值,而不是布尔值。这是不正确的;在JavaScript中,
&&
不一定会产生布尔值结果。但在所有情况下,这种语法都是错误的做法,正确吗?因为代码可读性,我从来没有听说过它被称为错误做法,而且在我看来,它读起来很好。但不是再说一次,我已经习惯看到这种事情了。
var str='My dummy string';
var str2='My other dummy string';
var falsy1= 1==2;
var truthy1= true;
var truthy2= true;
var falsy2= false;
console.log('Both bool true:', truthy1 && truthy2); // <== Both bool true: true
console.log('Bool true and string:', truthy1 && str); // <== Bool true and string: My dummy string
console.log('String and bool true:', str && truthy1); // <== String and bool true: true
console.log('Falsy operation and string:', falsy1 && str); // <== Falsy operation and string: false
console.log('Bool false and string:', falsy2 && str); // <== Bool false and string: false
console.log('Both bool false and true:', falsy1 && truthy1); // <== Both bool false and true: false
console.log('Both bool true and false:', truthy1 && falsy1); // <== Both bool true and false: false
console.log('Operation false and bool true:', falsy2 && truthy1); // <== Operation false and bool true: false
console.log('Operation false and bool false:', falsy2 && falsy1); // <== Operation false and bool false: false
console.log('Both strings:', str2 && str); // <== Both strings: My dummy string
console.log('Both strings:', str && str2); // <== Both strings: My other dummy string   
console.log('String and bool false:',  str && falsy1); // <== String and bool false: false  
console.log('String and 2 bool false and true:',  str && falsy1  && truthy1); // <== String and 2 bool false and true: false
console.log('3 bool false and true and true:',  falsy1 && truthy1 && truthy2); // <== 3 bool false and true and true: false
console.log('3 bool false and false and true:',  falsy1 && falsy1 && truthy1); // <== 3 bool false and false and true: false
console.log('Bool false and operation false and bool true:',  falsy1 && falsy2 && truthy1); // <== Bool false and operation false and bool true: false
console.log('3 bool true and true and false:',  truthy2 && truthy1 && falsy1); // <== 3 bool true and true and false: false
console.log('String and 2 bool false and true:',  str && falsy1 && truthy1); // <== String and 2 bool false and true: false
console.log('String and 2 bool true and false:',  str && truthy1 && falsy1); // <== String and 2 bool true and false: false
console.log('2 bool false and true and string:',   falsy1 && truthy1 && str); // <== 2 bool false and true and string: false
console.log('2 bool true and false string:',  truthy1 && falsy1 && str); // <== 2 bool true and false string: false
console.log('Bool true and string and bool false:',  truthy1 && str && falsy1); // <== Bool true and string and bool false: false
console.log('Bool false and string and bool true:',  falsy1 && str && truthy1); // <== Bool false and string and bool true: false
console.log('The existence of a string:',  !!str); // <== The existence of a string: true