JavaScript 中 == 和 === 有什么区别?

发布于 2024-12-15 21:33:24 字数 587 浏览 2 评论 0原文

可能的重复:
Javascript === 与 == :哪个重要我使用“等于”运算符?
什么时候 JavaScript == 比 === 更有意义? < /p>

是将字符串与未定义值进行比较时以下方法之间的区别。

 var x; 
 if(x==undefined) 
 { 
  alert(x); 
 }

为什么

if(x===undefined)
{ 
  alert(x); 
}

在这种情况下我应该更喜欢第二种方法..请让我知道优点..

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
When would JavaScript == make more sense than ===?

What is the difference between below methods in comparing a string with undefined value.

 var x; 
 if(x==undefined) 
 { 
  alert(x); 
 }

and

if(x===undefined)
{ 
  alert(x); 
}

Why should i prefer second method in this case.. Please let me know advantages..

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

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

发布评论

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

评论(4

不疑不惑不回忆 2024-12-22 21:33:24
  • == 尝试将值转换为相同类型,然后测试它们是否相同。 "5" == 5
  • === 不会这样做;它要求对象具有相同类型才相等。 "5" !== 5

在这种情况下,结果是:

  • 如果 xundefined,x == undefined 将为 truenull
  • 仅当 xundefined 时,x === undefined 才会为 true。

如果您希望同等对待 undefined 和 null,您应该更喜欢第一种方法。其常见用途之一是可选函数参数。

function greet(name, greeting) {
    if (name == undefined) name = 'World';
    if (greeting == undefined) greeting = 'Hello';
    alert(greeting + ' ' + name);
}

greet(); // alerts "Hello World"
greet("Bob"); // alerts "Hello Bob"
greet(null, "Goodbye"); // alerts "Goodbye World"
  • == attempts to convert the values to the same type before testing if they're the same. "5" == 5
  • === does not do this; it requires objects to be of the same type to be equal. "5" !== 5

In this case, the result is:

  • x == undefined will be true if x is undefined or null.
  • x === undefined will only be true if x is undefined.

You should prefer the first method if you'd like undefined and null to be treated equivalently. One common use of this is optional function arguments.

function greet(name, greeting) {
    if (name == undefined) name = 'World';
    if (greeting == undefined) greeting = 'Hello';
    alert(greeting + ' ' + name);
}

greet(); // alerts "Hello World"
greet("Bob"); // alerts "Hello Bob"
greet(null, "Goodbye"); // alerts "Goodbye World"
你是年少的欢喜 2024-12-22 21:33:24

假设我们有 x=5,

== 等于

x==8 为 false
x==5 为 true

=== 完全等于(值和类型)

x===5 为 true
x==="5" 是 false

希望你理解这个概念

suppose we have x=5,

== is equal to

x==8 is false
x==5 is true

=== is exactly equal to (value and type)

x===5 is true
x==="5" is false

Hope you understand this concept

疑心病 2024-12-22 21:33:24

=== 也会检查相同的类型。通过几个例子您就会明白:

(1 == '1') //Returns true

由于 == 不关心类型,因此返回 true。但是,如果您想要严格的类型检查,则可以使用 === 因为只有在类型相同且值相同时才返回 true。

(1 === '1') //Returns false
(1 === 1) //Returns true
  • 当两个字符串具有相同的字符序列、相同的长度以及对应的字符相同时,它们是严格相等的
    职位。
  • 当两个数字在数值上相等(具有相同的数值)时,它们严格相等。 NaN 不等于任何内容,
    包括 NaN。正零和负零彼此相等。
  • 如果两个布尔操作数都为 true 或都为 false,则它们严格相等。
  • 如果两个对象引用同一个对象,那么它们是严格相等的。
  • Null 和未定义类型是 ==(但不是 ===)。

参考

=== checks for the same type as well. You'll understand with a few examples:

(1 == '1') //Returns true

Since == doesn't bother with types, that returns true. However, if you want strict type checking, you'd use === because that returns true only if the it's of the same type, and is the same value.

(1 === '1') //Returns false
(1 === 1) //Returns true
  • 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 ===).

Reference

情绪少女 2024-12-22 21:33:24

== 只是比较两个值,如果它们是不同类型,则完成类型转换

=== 比较值及其类型 - 因此这里不会进行类型转换。

== is just comparing the two values, and if they are of different types, type conversion is done

=== compares the values and well as their types - so no type conversion will be done here.

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