简体   繁体   English

JavaScript将变量中的&&语句组合为true或false

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

just a quick question. 只是一个简单的问题。 I cannot find anything relating to this since I don't really see how to explain it... but, if I combine two bool values using an && to make another variable, what will happen? 我找不到与此相关的任何内容,因为我真的没有看到如何解释它...但是,如果我使用&&组合两个bool值来制作另一个变量,会发生什么?

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

If isEnabled() is false and isSupported() is true, will it equal false? 如果isEnabled()为false且isSupported()为true,它是否等于false?

In Javascript the && and || 在Javascript中&&|| operators are slightly strange. 运营商有点奇怪。 It depends on if the value is "falsy" (zero, undefined , null , empty string, NaN ) or truthy (anything else, including empty arrays). 这取决于值是否为“falsy”(零, undefinednull ,空字符串, NaN )或truthy(其他任何东西,包括空数组)。

With && if the first value is "falsy", then the result of the operation will be the first value, otherwise it will be the second value. 使用&&如果第一个值是“falsy”,则操作的结果将是第一个值,否则它将是第二个值。 With || 随着|| if the first value is "falsy" then the result of the operation will be the second value, otherwise it will be the first value. 如果第一个值是“falsy”,那么操作的结果将是第二个值,否则它将是第一个值。

Example: 例:

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

This is very useful if you want to replace this: 如果你想要替换它,这非常有用:

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

With: 附:

x = x || 5;

So in short, if isEnabled() is truthy then is_enabled will be set to whatever isSupported() returns. 所以简而言之,如果isEnabled()是真的,则is_enabled将被设置为isSupported()返回的任何内容。 If isEnabled() is falsy, then is_enabled will be set to whatever that falsy value is. 如果isEnabled()是假的,则is_enabled将被设置为该值的值。

Also as Robert pointed out, there is short-circuiting: 罗伯特指出,还有短路:

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

In both cases, the infinite_loop() call doesn't happen, since the two operations are short-circuited - || 在这两种情况下, infinite_loop()调用都不会发生,因为这两个操作是短路的 - || doesn't evaluate the second value when the first value is truthy, and && doesn't evaluate the second value when the first value is falsy. 当第一个值是真值时,不评估第二个值,而当第一个值是假的时, &&不评估第二个值。

false && true的结果是false

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

If any operand of && operator is falsy ( false , 0, null , undefined , NaN , "" ) then is_enabled will be assigned the first falsy value. 如果&&运算符的任何操作数是假的false ,0, nullundefinedNaN"" ),则is_enabled将被赋予第一个假值。

If all operands of && operator is not falsy , then the last operand will be assigned to is_enabled . 如果&&运算符的所有操作数都不是假的 ,那么最后一个操作数将被赋值给is_enabled

is_enabled would only be set to true if isEnabled and isSupported are both true. 如果isEnabled和isSupported都为true,则is_enabled将仅设置为true。 So if isEnabled is false, and isSupported is true, is_enabled would be false. 因此,如果isEnabled为false,并且isSupported为true,则is_enabled将为false。

yes: 是:

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

function isSupported() {
    return true;
}

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

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

if both functions return only true or false, then it just works as a normal && with booleans. 如果两个函数只返回true或false,那么它只是作为普通&&与布尔值一起工作。

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

First of all, && is only true if and only if both expressions are true. 首先,当且仅当两个表达式都为真时,&&才是真的。

So back to your question, true && false will equal to false, so yes. 所以回到你的问题,真正的&& false将等于false,所以是的。

You can also try to test these expressions yourself using the console function on firebug or chrome developer tools. 您还可以尝试使用firebug或chrome开发人员工具上的控制台功能自行测试这些表达式。

What you can do it's to just add a single & and apply an AND operation to those booleans, making it that if both of them are true, then is_enabled will be true. 你可以做的只是添加一个&并对这些布尔值应用AND操作,使得如果它们都为真,则is_enabled将为真。

var is_enabled = isEnabled() & isSupported();

EDIT Thanks to Pointy to pointing out that my syntax is incorrect, this should apply to C language, guess i just got confused 编辑感谢Pointy指出我的语法不正确,这应该适用于C语言,猜猜我只是感到困惑

Here you can see what are the possible cases of tests: 在这里您可以看到可能的测试案例:

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

then: 然后:

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

And the bonus: 奖金:

console.log('The existence of a string:',  !!str); // <== The existence of a string: true

Short Answer: If first is not falsy then second, else first. 简答如果第一个不是假,那么第二个,否则首先。

example : if isEnabled() returns false, then false is the result. 示例:如果isEnabled()返回false,则结果为false

otherwise If isEnabled() is true, then whatever isSupported() returns is the result. 否则如果isEnabled()为true,那么返回的isSupported()返回的结果。

Now false and true were used to simplify the answer, false can be any falsy value. 现在使用falsetrue来简化答案,false可以是任何虚假值。

Examples of truthy and falsy values: truthy和falsy值的示例:

var string = ""; var string =“”; // <-- falsy // < - falsy

var filledString = "some string in here"; var filledString =“这里有一些字符串”; // <-- truthy // < - truthy

var zero = 0; var zero = 0; // <-- falsy // < - falsy

var numberGreaterThanZero // <-- truthy var numberGreaterThanZero // < - truthy

var emptyArray = []; var emptyArray = []; // <-- truthy, we'll explore more about this next // < - truthy,我们将在下一步探索更多内容

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

The result of a successful Boolean operation such as "&&" is a Boolean value. 成功的布尔运算(例如“&&”)的结果是布尔值。 As such, the result of isEnabled() && isSupported() will be a Boolean value which will then be assigned to is_enabled 因此, isEnabled() && isSupported()将是一个布尔值,然后将其分配给is_enabled

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

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