• <i id='8VwGg'><tr id='8VwGg'><dt id='8VwGg'><q id='8VwGg'><span id='8VwGg'><b id='8VwGg'><form id='8VwGg'><ins id='8VwGg'></ins><ul id='8VwGg'></ul><sub id='8VwGg'></sub></form><legend id='8VwGg'></legend><bdo id='8VwGg'><pre id='8VwGg'><center id='8VwGg'></center></pre></bdo></b><th id='8VwGg'></th></span></q></dt></tr></i><div id='8VwGg'><tfoot id='8VwGg'></tfoot><dl id='8VwGg'><fieldset id='8VwGg'></fieldset></dl></div>
    • <bdo id='8VwGg'></bdo><ul id='8VwGg'></ul>
  • <tfoot id='8VwGg'></tfoot>
  • <small id='8VwGg'></small><noframes id='8VwGg'>

    1. <legend id='8VwGg'><style id='8VwGg'><dir id='8VwGg'><q id='8VwGg'></q></dir></style></legend>

        在 JavaScript 中,为什么是“0"?等于假,但是当通过“如果"测试时,它本身不是假的?

        In JavaScript, why is quot;0quot; equal to false, but when tested by #39;if#39; it is not false by itself?(在 JavaScript 中,为什么是“0?等于假,但是当通过“如果测试时,它本身不是假的?)

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

                  <small id='5YXhV'></small><noframes id='5YXhV'>

                • <tfoot id='5YXhV'></tfoot>

                  本文介绍了在 JavaScript 中,为什么是“0"?等于假,但是当通过“如果"测试时,它本身不是假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下说明 "0" 在 Javascript 中为 false:

                  The following shows that "0" is false in Javascript:

                  >>> "0" == false
                  true
                  
                  >>> false == "0"
                  true
                  

                  那么为什么下面会打印出"ha"?

                  So why does the following print "ha"?

                  >>> if ("0") console.log("ha")
                  ha
                  

                  推荐答案

                  原因是因为当你显式做 "0" == false 时,两边都被转换成数字,而 然后进行比较.

                  The reason is because when you explicitly do "0" == false, both sides are being converted to numbers, and then the comparison is performed.

                  当你这样做时:if ("0") console.log("ha"),字符串值正在被测试.任何非空字符串都是true,而空字符串是false.

                  When you do: if ("0") console.log("ha"), the string value is being tested. Any non-empty string is true, while an empty string is false.

                  相等 (==)

                  如果两个操作数不属于同一类型,JavaScript 会转换操作数,然后应用严格比较.如果任一操作数是数字或布尔值,则尽可能将操作数转换为数字;否则,如果任一操作数是字符串,则如果可能,另一个操作数将转换为字符串.如果两个操作数都是对象,那么当操作数引用内存中的同一个对象时,JavaScript 会比较相等的内部引用.

                  If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

                  (来自 Mozilla 开发者网络中的 比较运算符)

                  (From Comparison Operators in Mozilla Developer Network)

                  这篇关于在 JavaScript 中,为什么是“0"?等于假,但是当通过“如果"测试时,它本身不是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

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

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

                            <tfoot id='E18Np'></tfoot>
                            <legend id='E18Np'><style id='E18Np'><dir id='E18Np'><q id='E18Np'></q></dir></style></legend>
                              <tbody id='E18Np'></tbody>