JavaScript 中 == 和 === 的区别

发布于 2024-07-13 06:36:58 字数 121 浏览 7 评论 0原文

JavaScript 中 ===== 有什么区别? 我还见过 !=!== 运算符。 还有更多这样的运营商吗?

What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-07-20 06:36:58

看看这里:http://longgoldenears.blogspot.com/ 2007/09/triple-equals-in-javascript.html

3个等号的意思是“没有类型强制的相等”。 使用三重等于时,值的类型也必须相同。

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html

The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
灯角 2024-07-20 06:36:58

===!== 是严格比较运算符:

JavaScript 同时具有严格和
类型转换相等比较。
为了严格相等,对象是
比较必须具有相同的类型并且:

  • 当两个字符串具有相同的序列时,它们严格相等
    字符、长度相同、相同
    相应位置的字符。
  • 当两个数在数值上相等时,它们严格相等(有
    相同的数值)。 NaN 不是
    等于任何值,包括 NaN
    正零和负零相等
    彼此。
  • 如果两个布尔操作数都为 true 或
    两者都是假的。
  • 如果两个对象引用同一个对象,则它们严格相等。
  • NullUndefined 类型是 ==(但不是 ===)。 [即 (Null==Undefined) 为 true,但 (Null===Undefined) 为 false]< /里>

< a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators" rel="noreferrer">比较运算符 - MDC

=== and !== are strict comparison operators:

JavaScript has both strict and
type-converting equality comparison.
For strict equality the objects being
compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of
    characters, same length, and same
    characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have
    the same number value). NaN is not
    equal to anything, including NaN.
    Positive and negative zeros are equal
    to one another.
  • Two Boolean operands are strictly equal if both are true or
    both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

Comparison Operators - MDC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文