Before we start to get familiar with JavaScript corner cases, I’d like to make a distinction between Corner Case and Edge Case.

We can say that Edge Case is an issue that can occur only at minimum or maximum parameters. Predicting it can be a significant task since those situations may be neglected or underestimated. For example, a PC at full power can overheat, and its performance may deteriorate a little.

I’d love to introduce a Boundary Case also (a subject of the doubt too). It can occur if one of the parameters is beyond the minimum or maximum limits.

What about Corner Cases? I won’t give any definition since you’ll be able to do by yourself after you take a look at the next examples.


You won’t believe this!

If I ask you can something be coercive equal to the negation of itself, what would be your answer? You’d probably said that is a nonsense question

You may think that JS is a crazy language, and this should not happen for a popular language like JS. This example is silly because you will never compare value to the negation of itself in a real example. But this is a great example to help you clarify and adopt the right mental model!

You won’t ever have a situation where are you comparing array with negative arrays. You won’t ever design code on this way. That is an excellent example of how you don’t want to manage the codebase.

In the next example, I’ll explain what happens in great details, so you can have a clear image of what an algorithm is doing:

Firstly, I’ll refer to the documentation. On line 6, we have a comparison of primitive and non-primitive value. In this case, the rule №11 applied. The result of this algorithm is an empty string.

In the next step, we’re comparing an empty string with false. According to the algorithm, rule №9 applied. The next step (line 8) is to apply rule №5. The 5th step is to compare two numbers. Since we’re using equality comparison, we’re going to call strict equality comparison algorithm.

The last step is to return a _true _from the strict equality comparison. The second example is a bit more practical because we’re using not equals (double equality negation) - checking if is not coercively equal:

As we are comparing two non-primitive types, it means that we’re going to perform an identity comparison. The same applies when using strict equality comparison.

#programming-tips #javascript-development #programming #javascript-tips #javascript

How to handle comparison corner cases
1.85 GEEK