JavaScript 将变量中的 && 语句组合为真或假

JavaScript combining && statements in a variable to be true or false

本文关键字:amp 组合 语句 变量 JavaScript      更新时间:2023-12-04

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

var is_enabled = isEnabled() && isSupported();
如果 isEnabled(( 是假

的,而 isSupported(( 是真的,它会等于假吗?

在Javascript中,&&||运算符有点奇怪。这取决于值是"假的"(零、undefinednull、空字符串、NaN(还是真(其他任何东西,包括空数组(。

&&如果第一个值是"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;
}

跟:

x = x || 5;

因此,简而言之,如果isEnabled()是真实的,那么is_enabled将被设置为isSupported()返回的任何内容。如果isEnabled()是假的,那么is_enabled将被设置为该假值。

正如罗伯特指出的那样,存在短路:

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

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

false && true的结果是false

如果 isEnabled(( 为 false,并且您使用 &&&,则永远不会调用 isSupported((,因为评估会短路。

如果&&运算符的任何操作数是的(false, 0, nullundefinedNaN""(,那么is_enabled将被赋予第一个假值。

如果&&运算符的所有操作数都不是的,则最后一个操作数将分配给is_enabled

是的:

<script type="text/javascript">
function isEnabled() {
    return false;
}
function isSupported() {
    return true;
}
var is_enabled = isEnabled() && isSupported();
alert(is_enabled);  // = 'false'
</script>

如果两个函数都只返回真或假,那么它只是像布尔值的正常&&工作。

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

首先,&&仅当且仅当两个表达式都为真时才为真。

所以

回到你的问题,真&假等于假,所以是的。

您也可以尝试使用Firebug或chrome开发者工具上的控制台功能自行测试这些表达式。

只有当 isEnabled 和 isSupport 都为 true 时,is_enabled才会设置为 true。因此,如果 isEnabled 为假,而 isSupport 为真,则is_enabled为假。

你可以做的是添加一个&,并对那些布尔值应用一个AND运算,如果它们都是真的,那么is_enabled就是真的。

var is_enabled = isEnabled() & isSupported();

编辑感谢 Pointy 指出我的语法不正确,这应该适用于 C 语言,猜猜我只是感到困惑

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

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

简短回答:如果第一个不是假的,那么第二个,否则先。

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

否则,如果 isEnabled(( 为 true,那么无论什么 isSupported(( 返回都是结果。

现在falsetrue被用来简化答案,假可以是任何虚假值。

真实值和虚假值的示例:

变量字符串 = ";<--假

var filledString = "some string in here";//<-- truthy

var zero = 0;//<-- falsy

var numberGreaterThanZero//<-- truthy

var emptyArray = [];//<-- 说实话,我们接下来会探讨更多关于这个

var emptyObject = {};//<-- truthy

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