标签云

微信群

扫码加入我们

WeChat QR Code

这等于运算符(= =和= = =)应使用JavaScript的比较?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement.

Is there a performance benefit to replacing == with ===?

Any performance improvement would be welcomed as many comparison operators exist.

If no type conversion takes place, would there be a performance gain over ==?


To whom it might be interested in the same subject === vs ==, but in PHP, can read here: stackoverflow.com/questions/2401478/why-is-faster-than-in-php/…

2018年05月24日41分50秒

Just in case anyone was wondering in 2012: === is way faster than ==. jsperf.com/comparison-of-comparisons

2018年05月24日41分50秒

minitech it should be as it does not do type conversion

2018年05月23日41分50秒

minitech, I doubt anyone is going to make their application noticeably faster by using === over ==. In fact, the benchmark doesn't show a big difference between both on modern browsers. Personally, I usually use == everywhere unless I really need strict equality.

2018年05月23日41分50秒

johndodo: Premature Optimization is only a thing because optimized code can be less ideal from a maintainability and readability standpoint, but if the type conversion features of == are not needed, the use of === instead is good practice, not premature optimization. There's nothing about === that degrades code maintainability or readability.

2018年05月23日41分50秒

=== is not quicker if the types are the same. If types are not the same, === will be quicker because it won't try to do the conversion.

2018年05月23日41分50秒

=== will never be slower than ==. They both do type checking, so === doesn't do anything extra compared to ==, but the type check may allow === to exit sooner when types are not the same.

2018年05月23日41分50秒

Replacing all ==/!= with ===/!== increases the size of the js file, it will take then more time to load. :)

2018年05月23日41分50秒

"...the rules by which they do that are complicated and unmemorable..." Now such statements make you feel so safe when programming...

2018年05月24日41分50秒

Sometimes JavaScript's type system makes me want to run away screaming.

2018年05月23日41分50秒

Software Monkey: not for value types (number, boolean, ...)

2018年05月24日41分50秒

type coercion vs type casting vs type convertion: stackoverflow.com/questions/8857763/…

2018年05月23日41分50秒

Since nobody has mentioned the Javascript Equality Table, here it is: dorey.github.io/JavaScript-Equality-Table

2018年05月23日41分50秒

In the first statement, are you sure that 'true' is converted to 1 and not 1 converted to true?

2018年05月24日41分50秒

Where do the terms "equality" and "identity" come from? The standard does not use those terms. It calls == "abstract equality" and it calls === "strict equality". Granted calling == any kind of "equality" is IMHO awful, since it is not transitive, but why quibble? I take more issue with "identity" though; I think that term is pretty misleading, though it "works." But seriously, who coined the term "identity"? I search the standard and could not find it.

2018年05月24日41分50秒

activa: I would clarify, that the strings are so equal only when they are literals. new String("abc") === "abc" is false (according to my research).

2018年05月23日41分50秒

new Number() == "0". Also in Firefox: (function(){}) == "function () {\n}"

2018年05月23日41分50秒

Thank you for explaining why new String("123") !== "123". They are different types. Simple, yet confusing.

2018年05月23日41分50秒

String objects behave as strings as does any other object. new String should never be used, as that doesn't create real strings. A real string and can be made with string literals or calling String as a function without new, for example: String(0); //"0", Real string, not an object

2018年05月23日41分50秒

But in the cases you detailed, the operator "==" behaves exactly the same.

2018年05月23日41分50秒

temple: Of course it is. x == y and y == x are the same!

2018年05月24日41分50秒

mfeineis you mean === or !== instead of == or != . Don't want to confuse new coders ;)

2018年05月23日41分50秒

vsync: Wow, those people who abuse JQuery also say the same thing. I never knew you had so much in common...

2018年05月23日41分50秒

vsync: If you really don't want types to be equal, you should use three equals!

2018年05月23日41分50秒

This answer is so damn useful!!!

2018年05月24日41分50秒

Result of Type(x) is implied to be the same as typeof ?

2018年05月24日41分50秒

David: correct. That's why this answer is inaccurate (or even wrong)

2018年05月23日41分50秒

David var a = {}, b = {}; a == b returns false.

2018年05月23日41分50秒

Yes: Two different objects with the same type and value compare false, i.e., this answer is just wrong. Why does it have 50 upvotes?

2018年05月23日41分50秒

I realize this is old, but to clarify why this answer is still "correct" is because in the example var a = {}, b = {}; While both a and b is indeed both an object, but they are not the same value, technically speaking. They are different instances. Note that comparing instances behaves differently than comparing primitives. Which probably adds to this confusion. You will see similar comparison behavior if you use instance version of primitive data types. E.g new String('asdf') or new Number(5). Ex: new Number(5) == new Number(5) is false, even though they hold the same value.

2018年05月23日41分50秒

More than type safety you want logical correctness - sometimes you want things to be truthy when == disagrees.

2018年05月23日41分50秒

Now, how do these compare when there is an actual type coersion for == operator? Remember, that's when there's a performance boost.

2018年05月23日41分50秒

MAJOR difference when tested properly for the aforementioned reasons of quicker to only check type inequality. jsfiddle.net/4jhuxkb2

2018年05月24日41分50秒

Here is a detailed article on comparison of === vs ==

2018年05月23日41分50秒

also, 'string' !== 'number'

2018年05月24日41分50秒

I don't understand why the string arrow is pointing to the big gray box, is it supposed to mean the interrupter is casting the string to a number?

2018年05月24日41分50秒

vsync It points to the string option within the grey box i.e string -> # || NaN. Javascript is not a type-script language i.e basically it can have any type of variable. So, it is pointed to that grey box.

2018年05月23日41分50秒

I simply asked if it is for casting purposes since the string is supposed to be compared to a type number, so the interrupter looks at what the string should be compared to and cast the string accordingly?

2018年05月23日41分50秒

The big gray box is what ToNumber would return when given different types, so if it is given a string it will only choose the last option (and convert it to a number). == uses ToNumber only in the cases string == number or boolean == anything above (and only on the string/boolean). This means == will never convert undefined or null even though they are in the gray box. (For any combination of either undefined or null or both, == will always return true. Also, whether a value is on the left or right side doesn't matter, == (and ===) will return the same result.)

2018年05月23日41分50秒

Your looseEqual is wrong. Function == Function.toString() is true, but looseEqual(Function, Function.toString()) is false. Not sure why you filter out functions at the beginning.

2018年05月24日41分50秒

Oriol you were right, I updated the code to account for that, FYI based on my tests it wasn't enough to remove the filter for "functions", instead "functions" had to be handled differently altogether.

2018年05月24日41分50秒

Be aware the spec doesn't treat functions differently, they are just objects. The problem seems that you rely on typeof x === "object" to check if it's an object, but `typeof only works for non-null primitives. You might be interested in my list of proper ways to check if a value is an object

2018年05月23日41分50秒

I tried treating functions and objects the same but found he results were incorrect. For example if functions were treated like objects then comparing a function with an object that implements valueOf() or toString() function that matches the function would pass but in reality it doesn't. Example: (function blah() { console.log("test"); }) != {valueOf:function(){return "function blah() { console.log(\"test\"); }";}} - check out this JS Fiddle which runs all the tests: jsfiddle.net/luisperezphd/7k6gcn6g (there 1,225 test permutations)

2018年05月24日41分50秒

Interesting. But note your code fails with {toString:function(){return null;}}, and some ES6 thingies. See jsfiddle.net/7k6gcn6g/1

2018年05月23日41分50秒

Not always true. With gzip compression, the difference would be almost negligible.

2018年05月23日41分50秒

Agree, but thousand "===" means also 10 thousands of code lines else so 1kb more or less... ;)

2018年05月23日41分50秒

f your that concerned about size then just swap all your == with ===, then use a regexp wrapped in av eval to switch it back

2018年05月24日41分50秒

Here is a detailed article on comparison of === vs ==

2018年05月23日41分50秒

In JavaScript, this is completely wrong and wrongly incomplete. 0 != null. -1

2018年05月23日41分50秒

0 == null is false.

2018年05月23日41分50秒