Does it make sense to use === to compare strings in JavaScript?

It is clear how using the === operator, for example. numbers are useful ( 0 , null and undefined are all false values, which can lead to confusion), I'm not sure if there are any advantages to using === to compare strings.

Some of my teammates use this operator for all comparisons, but does it really make sense? Is there at least a slight impact on performance?

+4
source share
2 answers

If you know that the types are the same, then there is no difference in algorithm between == and === .

However, when I see == , I assume that I am using it for my type of coercion, which forces me to stop and analyze the code.

When I see === , I know that there is no coercion, so I do not need to think about it.

In other words, I only use == if I intend to use some kind of coercion type.

+9
source

It is considered good practice to always use === for comparison in JavaScript. Because JavaScript is a dynamic language, it is possible that you think two strings can be variables with different types.

Consider the following:

 var a = "2", b = 2; a == b // true a === b // false 

The only way to guarantee a false result in this case is to use === when comparing two values.

-2
source

All Articles