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

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

      <tfoot id='bBGFL'></tfoot>

          <bdo id='bBGFL'></bdo><ul id='bBGFL'></ul>
      1. JavaScript 中是 true == 1 和 false == 0 吗?

        时间:2023-08-07
      2. <i id='AuKns'><tr id='AuKns'><dt id='AuKns'><q id='AuKns'><span id='AuKns'><b id='AuKns'><form id='AuKns'><ins id='AuKns'></ins><ul id='AuKns'></ul><sub id='AuKns'></sub></form><legend id='AuKns'></legend><bdo id='AuKns'><pre id='AuKns'><center id='AuKns'></center></pre></bdo></b><th id='AuKns'></th></span></q></dt></tr></i><div id='AuKns'><tfoot id='AuKns'></tfoot><dl id='AuKns'><fieldset id='AuKns'></fieldset></dl></div>
          • <tfoot id='AuKns'></tfoot>
                <bdo id='AuKns'></bdo><ul id='AuKns'></ul>

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

                  <tbody id='AuKns'></tbody>

                  <legend id='AuKns'><style id='AuKns'><dir id='AuKns'><q id='AuKns'></q></dir></style></legend>
                  本文介绍了JavaScript 中是 true == 1 和 false == 0 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在阅读一本关于 JavaScript 的好书.

                  I was reading a good book on JavaScript.

                  开头是:

                  布尔类型只接受两个字面值:真和假.它们与数值不同,所以 true 不等于 1,false 不等于 0.

                  Boolean type take only two literal values: true and false. These are distinct from numeric values, so true is not equal to 1, and false is not equal to 0.

                  但是,我观察到以下情况:

                  However, I observed following:

                  if(1==true)
                    document.write("oh!!! that's true");  //**this is displayed**
                  

                  我知道,JavaScript 中的每种类型都有对应的布尔值.

                  I know, that every type in JavaScript has a Boolean equivalent.

                  但是,真相是什么?

                  推荐答案

                  确实 truefalse 在 Javascript 中不代表任何数值.

                  It's true that true and false don't represent any numerical values in Javascript.

                  在某些语言(例如 C、VB)中,布尔值被定义为实际数值,因此它们只是 1 和 0(或 -1 和 0)的不同名称.

                  In some languages (e.g. C, VB), the boolean values are defined as actual numerical values, so they are just different names for 1 and 0 (or -1 and 0).

                  在其他一些语言(例如 Pascal、C#)中,有一种独特的布尔类型,它不是数字.可以在布尔值和数值之间进行转换,但不会自动发生.

                  In some other languages (e.g. Pascal, C#), there is a distinct boolean type that is not numerical. It's possible to convert between boolean values and numerical values, but it doesn't happen automatically.

                  Javascript 属于具有独特布尔类型的类别,但另一方面,Javascript 非常热衷于在不同数据类型之间转换值.

                  Javascript falls in the category that has a distinct boolean type, but on the other hand Javascript is quite keen to convert values between different data types.

                  例如,即使数字不是布尔值,您也可以在需要布尔值的地方使用数值.使用 if (1) {...}if (true) {...} 一样有效.

                  For example, eventhough a number is not a boolean, you can use a numeric value where a boolean value is expected. Using if (1) {...} works just as well as if (true) {...}.

                  在比较值时,例如在您的示例中, == 运算符和 === 运算符之间存在差异.== 相等运算符很高兴地在类型之间进行转换以找到匹配项,因此 1 == true 的计算结果为 true,因为 true 被转换为 1.=== 类型相等运算符不进行类型转换,因此 1 === true 计算结果为 false,因为值属于不同类型.

                  When comparing values, like in your example, there is a difference between the == operator and the === operator. The == equality operator happily converts between types to find a match, so 1 == true evaluates to true because true is converted to 1. The === type equality operator doesn't do type conversions, so 1 === true evaluates to false because the values are of different types.

                  这篇关于JavaScript 中是 true == 1 和 false == 0 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <legend id='9Zdz5'><style id='9Zdz5'><dir id='9Zdz5'><q id='9Zdz5'></q></dir></style></legend>

                    <small id='9Zdz5'></small><noframes id='9Zdz5'>

                    • <bdo id='9Zdz5'></bdo><ul id='9Zdz5'></ul>

                      • <tfoot id='9Zdz5'></tfoot>

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