0

I'm newbie in JS, I know that == double comparation for equality and === triple one for equality and type. My teacher says that I should use always triple. However, I don't see any down effect if I use double equal when one of the elements is a string.

'' == 0 // true
'10' == 10 // true
'dog' == 'cat' // false

So, I'm using always double equal when some of the element is a string (avoiding this 'false' == false) and triple when I'm detecting null or undefined.

Is there any down side for my approach?

I checked this previous answer: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Johnny Johnny
  • 319
  • 2
  • 11
  • Listen to your teacher. It will make your life a lot easier in the long run. Good luck :) – Nick May 28 '20 at 17:44
  • The performance benefits are negligible compared to the readability and obviousness. Do you want to go look up the `==` table every time you encounter a bug that *might* be related to equality to double check? Better question is whether the developer that inherits your code will understand and know to look for the table when they encounter the related bug. Just use `===`. – zero298 May 28 '20 at 17:45
  • Hi @Johny Johny it is as simple as that if you are only concerned with the content to compare then good to go with == and if you need to check the type comparison also then go for === as simple as that. – Charanjeet Singh May 28 '20 at 17:46
  • jajaja thanks Nick, well at the end with === I don't need to think – Johnny Johnny May 28 '20 at 18:20
  • thanks zero298 ^^ I spot a == in [jquery](https://github.com/jquery/jquery/blob/master/src/core/access.js), line 9! Ok I will type a little more, cool I didn't know that thing about performance! – Johnny Johnny May 28 '20 at 18:24
  • Wow, @Charanjeet Singh that's true, If I'm concerned with the content to compare I can use it, great!! Thanks a lot! – Johnny Johnny May 28 '20 at 18:27

4 Answers4

2

If you read the linked answer, you also read this chunk of code involving strings:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

Those are string, but still the behaviour is not exactly what you may expect. Indeed using === can make your life easier as your teacher said :)

Balastrong
  • 4,336
  • 2
  • 12
  • 31
2

If you use '10' == 10 , then the js interpreter must do a type conversion. This costs a little performance.

If you are using code quality tools like sonarqube, you will get some 'hints' to fix it.

Marc
  • 1,836
  • 1
  • 10
  • 14
2

Using my personal working experience as a basis, I have never user the abstract equality operator(==) over the strict equality operator(===).

Since the == operator may have some unpredictable behavior, like

'0' == false    // true

While

'0' === false   // false

The second case is normally what you want your code to do.

Also, the == operator is more performance-costly than the === operator.

You can read more about this on the ecmaScript documentation:

The Equals(==) Operator: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.1

The Abstract Equality Comparison Algorithm: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

The Strict Equals(===) Operator: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.4

The Strict Equality Comparison Algorithm: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6 (these two comparison algorithm probably will clarify to you why the strict equality is better on performance)

innis
  • 370
  • 1
  • 3
  • 14
1

It depends on the ocassion.

For example if you try to check if 9 == '9' it would return true, but they are in fact different, because one of then is a word(string) and the other is number(integer). In some of the cases that would make you suffer later.

The best scenario is to know what you are expecting it to be - is it a string, integer or something else and use the === or atleast check the type of the variable.

Mitko Delibaltov
  • 486
  • 1
  • 6
  • 16