<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;'有什么区别和'||'通过三元运算符('?'和':')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                    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>