<i id='bJh76'><tr id='bJh76'><dt id='bJh76'><q id='bJh76'><span id='bJh76'><b id='bJh76'><form id='bJh76'><ins id='bJh76'></ins><ul id='bJh76'></ul><sub id='bJh76'></sub></form><legend id='bJh76'></legend><bdo id='bJh76'><pre id='bJh76'><center id='bJh76'></center></pre></bdo></b><th id='bJh76'></th></span></q></dt></tr></i><div id='bJh76'><tfoot id='bJh76'></tfoot><dl id='bJh76'><fieldset id='bJh76'></fieldset></dl></div>
    <tfoot id='bJh76'></tfoot>
      <legend id='bJh76'><style id='bJh76'><dir id='bJh76'><q id='bJh76'></q></dir></style></legend>

      <small id='bJh76'></small><noframes id='bJh76'>

        • <bdo id='bJh76'></bdo><ul id='bJh76'></ul>

        使用'&amp;&amp;'有什么区别和'||'通过三元运算符('

        What is the difference between using #39;amp;amp;#39; and #39;||#39; over a ternary operator (#39;?#39; and #39;:#39;)?(使用amp;amp;有什么区别和||通过三元运算符(?和:)?)

      1. <legend id='87Al8'><style id='87Al8'><dir id='87Al8'><q id='87Al8'></q></dir></style></legend>

        <small id='87Al8'></small><noframes id='87Al8'>

          <tbody id='87Al8'></tbody>
          • <bdo id='87Al8'></bdo><ul id='87Al8'></ul>

            1. <i id='87Al8'><tr id='87Al8'><dt id='87Al8'><q id='87Al8'><span id='87Al8'><b id='87Al8'><form id='87Al8'><ins id='87Al8'></ins><ul id='87Al8'></ul><sub id='87Al8'></sub></form><legend id='87Al8'></legend><bdo id='87Al8'><pre id='87Al8'><center id='87Al8'></center></pre></bdo></b><th id='87Al8'></th></span></q></dt></tr></i><div id='87Al8'><tfoot id='87Al8'></tfoot><dl id='87Al8'><fieldset id='87Al8'></fieldset></dl></div>
              <tfoot id='87Al8'></tfoot>

                1. 本文介绍了使用'&amp;&amp;'有什么区别和'||'通过三元运算符('?'和':')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在JavaScript中,使用和使用有什么区别

                  In JavaScript, what is the difference between using

                  true ? 'Hello' : 'Goodbye' //Evaluates to "Hello"
                  
                  false ? 'Hello' : 'Goodbye' //Evaluates to "Goodbye"
                  

                  true && 'Hello' || 'Goodbye' //Evaluates to "Hello"
                  
                  false && 'Hello' || 'Goodbye' //Evaluates to "Goodbye"
                  

                  推荐答案

                  这两个不同的概念恰好给你相同的答案.

                  These are two different concepts that happen to give you the same answer.

                  第一个示例使用 三元运算符 并且其结果取决于第一个操作数(在您的示例中为 true/false):

                  The first example uses the ternary operator and its result depends only on the first operand (in your example true/false):

                  true ? 'Hello' : 'Goodbye' //Evaluates to "Hello"
                  false ? 'Hello' : 'Goodbye' //Evaluates to "Goodbye"
                  

                  它是 if/else 的简写形式.如果第一个操作数为真,则返回第二个操作数 (Hello).如果第一个操作数是假的,则返回第三个操作数 (Goodbye).

                  It is a shorthand form of an if/else. If the first operand is truthy, return the second operand (Hello). If the first operand is falsy, return the third operand (Goodbye).

                  你的第一个例子的第一个表达式可以重写为:

                  The first expression of your first example can be rewritten as:

                  if (true)
                      return 'Hello';
                  else
                      return 'Goodbye';
                  

                  <小时>

                  第二个示例使用 逻辑运算符 并且其结果取决于第一个和第二个操作数.


                  The second example makes use of logical operators and its result depends on both the first and the second operand.

                  true && 'Hello' || 'Goodbye' //Evaluates to "Hello"
                  false && 'Hello' || 'Goodbye' //Evaluates to "Goodbye"
                  

                  如果 firstOperand &&secondOperand 求值为真值,返回 secondOperand.如果他们评估为虚假,则返回 thirdOperand (Goodbye).

                  If firstOperand && secondOperand evaluates to a truthy value, return secondOperand. If they evaluate to something falsy, return thirdOperand (Goodbye).

                  在您的示例中,由于非空字符串是真实的,因此 true &&'Hello' 计算结果为 Hello,并且 false &&'Hello' 计算结果为 false.

                  In your examples, since a non-empty string is truthy, true && 'Hello' evaluates to Hello, and false && 'Hello' evaluates to false.

                  所以你的第二个例子变成了:

                  So your second example turns into:

                  'Hello' || 'Goodbye'
                  false || 'Goodbye'
                  

                  因为 || 有效,恰好输出您的第一个示例输出的内容,但它们是不同的概念.

                  Because of how || works, that happens to output what your first example outputs, but they're different concepts.

                  请注意,在第一个示例中,我们知道在计算第一个操作数后要返回什么.在第二个示例中,我们必须先计算前 两个 操作数,然后才能知道要返回什么.

                  Notice how in the first example, we know what to return after evaluating the first operand. In the second example, we have to evaluate the first two operands before we know what to return.

                  这篇关于使用'&amp;&amp;'有什么区别和'||'通过三元运算符('?'和':')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                    1. <tfoot id='YHEDU'></tfoot>

                      <small id='YHEDU'></small><noframes id='YHEDU'>

                        <bdo id='YHEDU'></bdo><ul id='YHEDU'></ul>
                      • <legend id='YHEDU'><style id='YHEDU'><dir id='YHEDU'><q id='YHEDU'></q></dir></style></legend>
                          <tbody id='YHEDU'></tbody>
                      • <i id='YHEDU'><tr id='YHEDU'><dt id='YHEDU'><q id='YHEDU'><span id='YHEDU'><b id='YHEDU'><form id='YHEDU'><ins id='YHEDU'></ins><ul id='YHEDU'></ul><sub id='YHEDU'></sub></form><legend id='YHEDU'></legend><bdo id='YHEDU'><pre id='YHEDU'><center id='YHEDU'></center></pre></bdo></b><th id='YHEDU'></th></span></q></dt></tr></i><div id='YHEDU'><tfoot id='YHEDU'></tfoot><dl id='YHEDU'><fieldset id='YHEDU'></fieldset></dl></div>