我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。

能否取代 = = = = = = =?

任何性能改进都将被欢迎,因为有许多比较运营商。

如果没有类型转换发生,会有超越 ==的性能收益吗?


当前回答

原因在上面的答案中被解释,而且杜格拉斯·克罗克福德也对此非常清楚(JavaScript:The Good Parts)。

但是,有一個例外: == null 是檢查“是零或不定義”的有效方式:

if( value == null ){
    // value is either null or undefined
}

例如,jQuery 1.9.1 使用此模式 43 次,而 JSHint 合成检查器甚至为此提供 eqnull 放松选项。

从 jQuery 风格指南:

严格的平等检查(===)应用于对 ==的支持,唯一的例外是通过零来检查非定义和零。

// Check for both undefined and null values, for some important reason. 
undefOrNull == null;

编辑 2021-03:

如今,大多数浏览器支持 Nullish coalescing 操作器(??) 和 Logical nullish 任务(??=),如果变量是 null 或 undefined,例如:

if (a.speed == null) {
  // Set default if null or undefined
  a.speed = 42;
}

可以用任何形式写作。

a.speed ??= 42;
a.speed ?? a.speed = 42;
a.speed = a.speed ?? 42;

其他回答

=== 检查相同的侧面在类型和值均等。


例子:

'1' === 1 // will return "false" because `string` is not a `number`

常见的例子:

0 == ''  // will be "true", but it's very common to want this check to be "false"

另一个常见的例子:

null == undefined // returns "true", but in most cases a distinction is necessary

很多时候,一个不类型的检查会很有用,因为你不在乎值是否不定义,零,0或“。

在这里的答案中,我没有读到什么是平等的意思,有些人会说 === 意味着平等和相同的类型,但这不是真的。

所以,让我们采取下列代码:

var a = [1,2,3];
var b = [1,2,3];
var c = a;

var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true

在这里相同:

var a = { x: 1, y: 2 };
var b = { x: 1, y: 2 };
var c = a;

var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true

var a = { };
var b = { };
var c = a;

var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true

这种行为并不总是显而易见的,有更多的故事,而不是平等和同类。

规则是:

对于值类型(数字): a === b 如果 a 和 b 具有相同的值,并且具有相同的类型,则返回真实。


下一篇:特殊案例...

var a = "12" + "3";
var b = "123";

alert(a === b); // returns true, because strings behave like value types

但是,这个问题怎么样呢?

var a = new String("123");
var b = "123";

alert(a === b); // returns false !! (but they are equal and of the same type)

我以为线条像值类型一样行事吗? 好吧,这取决于你问谁...... 在这种情况下, a 和 b 不是相同的类型. a 是类型对象,而 b 是类型线条。

“我要在JavaScript比较中使用 ==或 ===”的辩论是相同或类似于一个问题:“我要用一个“<unk>”或一个“<unk>”吃。

这个问题的唯一合理答案是:

您应该使用动态类型比较,例如:==为空白类型比较,您应该使用静态类型比较,例如:===为强大的类型比较。

这是因为他们不是相同的,他们没有相同的目的,并且不应该用于相同的目的。

當然,兩個「<unk>」和「<unk>」都是用於「吃」的,但你會選擇根據你所服用的食物使用它們。

是啊!重要。

=== 操作员在 javascript 中检查值以及类型,在那里作为 == 操作员只检查值(需要时进行类型转换)。

此分類上一篇

您可以轻松地测试它. 在 HTML 文件中插入跟踪代码并在浏览器中打开它

<script>

function onPageLoad()
{
    var x = "5";
    var y = 5;
    alert(x === 5);
};

</script>

</head>

<body onload='onPageLoad();'>

现在修改 onPageLoad() 方法以警告(x == 5);你会得到真相。

意思是平等 没有类型强制性 类型强制性 意思是 JavaScript 不会自动将任何其他数据类型转换为序列数据类型

0==false   // true,although they are different types

0===false  // false,as they are different types

2=='2'    //true,different types,one is string and another is integer but 
            javaScript convert 2 to string by using == operator 

2==='2'  //false because by using === operator ,javaScript do not convert 
           integer to string 

2===2   //true because both have same value and same types