Javascript: Difference between strict and lenient equality

Javascript: Difference between strict and lenient equality
4.88 (97.5%) 8 votes

  2 minutes

 

In Javascript everyone recommends to use strict equality instead of lenient. In this article we will see the difference between strict and lenient equality and discover why it should not be used. So lets begin.

2 == true  // 2 === 1
false

2 == false  // 2 === 0
false

1 == true  // 1 === 1
true

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

2 is not equal to true because lenient equality tries to convert true to number which is 1 and then does strict equality therefore 2 is not equal to 1.

2 is not equal to false also, why , because false is converted to 0 then 2 cannot be equal to 0.

1 is equal to true because true is converted to 1 thus it is true. Similarly false is equal to 0.

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

'abc' == true  // NaN === 1
false

Empty string is false because conversion of empty string to number is 0

Number('')  // 0

0 is equal to 0 because false is also converted to 0.

'abc' is converted to NaN by Number thus Nan is not equal to 1.

'\n\t123\r ' == 123  
true

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

In first line comes the confusion \n\t123\r is also converted to 123. Thus  it becomes true.

In second line its understandable with previous examples.

{} == '[object Object]'
true

['123'] == 123
true

[] == 0
true

In first case lenient equality tries to convert it to string i.e. String({}) which gives '[object Object]'. Thus it is true.

In Second case, again it convert to string String(['123']) = 123. Thus it also becomes equal.

In third case, agains come the problem this time it uses Number conversion Number([]) = 0 thus it also satisfies.

Confusing? Lets see how these works.

The algorithm for comparing via normal equality works as follows. If both operands have the same type (one of the six specification types—Undefined, Null, Boolean, Number, String, and Object), then compare them via strict equality.

Otherwise, if the operands are:

  1. undefined and null, then they are considered leniently equal:

    undefined == null
    true
  2. A string and a number, then convert the string to a number and compare both operands via strict equality.
  3. A boolean and a nonboolean, then convert the boolean to a number and compare leniently (again).
  4. An object and a number or a string, then try to convert the object to a primitive (via the algorithm described in Algorithm: ToPrimitive()—Converting a Value to a Primitive) and compare leniently (again).

Otherwise—if none of the aforementioned cases apply—the result of the lenient comparison is false.

This is taken from http://speakingjs.com/es5/ch09.html.

Now try finding the reasons for below and comment for others.

undefined==null
true

undefined===null
false

Try everything in console and explore.

 

Also read about Javascript’ Unknown fact and magic here

https://www.learnsteps.com/javascript-increasing-performance-by-handling-scopes-smartly/

https://www.learnsteps.com/javascript-increasing-performance-using-dynamic-loading/

https://www.learnsteps.com/javascript-confusing-concepts-and-uncertainities/

 

Liked the post, subscribe to stay updated of more such posts

 


Gaurav Yadav

Gaurav is a Full Stack Web Developer and Blogger. Sportsperson by heart and loves football. He has experience with various frameworks in php, python and javascript. Loves to explore new frameworks and evolve with the trending technology.

1 COMMENT
  • Mohammad Afzal
    Reply

    awesome work sir . keep sharing your experience

Leave a Reply

Your email address will not be published. Required fields are marked *