== vs === in javascript

== vs === in javascript

In this article we will learn in depth about == and === operator in javascript

🤞🤞Motto of Article 🤞🤞

🖐️in this article, you’ll learn about JavaScript == vs ===comparision operator

🖐️ our main concern is getting to know the difference between the ‘==’ and ‘===’ operators that the javascript provides, though they look similar, they are very different.

🤘without further delay, let's understand with examples.🚲🚲

👽Introduction

🖐️In daily life, we encounter multiple situations where we need to compare two things.

🖐️ The equality operator in javascript is used to compare if two values are equal. The comparison is made by == and === operators in javascript. The main difference between the == and === operators in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What is the type coercion ??👀

👀👀now you will wonder what is type coercion🤔

Note: Type coercion means that the two values are compared only after attempting to convert them into the same type. Let’s look at all the values in which the ‘==’ operator will return true.

Boolean values of literals during comparison:

True

‘0’,‘false’ ,[],{},function(){}

False

false,null,undefined,nan and emty string

🤔what is == operator??🤔

In Javascript, the ‘==’ operator is also known as the loose equality operator which is mainly used to compare two values on both sides and then return true or false. This operator checks equality only after converting both the values to a common type i.e type coercion.

   // '==' operator

    console.log(1 == '1');//true
    console.log("p"=="p");//true
    console.log(true == 1);//true
    console.log(false == 0);//true
    console.log(null == undefined);//true
    //false value
    console.log({} == {});//false
    console.log(23 == 22);//false

Output: 1)In the above code when we compare 1 with ’1’ the javascript will convert the ’1’ into the number value of 1, and hence we get true,

2)A similar thing happens when we try to check if ‘true == 1’, in that case, the javascript basically converts 1 into a truthy value and then compares and returns the boolean true.

3)The case of (null == undefined) is special as when we compare these values we get true. Both null and undefined are false values and they represent an ’empty’ value or undefined in js, hence the comparison with ‘==’ operator returns true.

4) console.log({} == {});//falseThe reason for this is that internally JavaScript has two different approaches for testing equality. Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference. That comparison by reference checks to see if the objects given refer to the same location in memory.

🤔what is === operator??🤔

  • Strict equality === checks whether two values are the same or not.

  • Value is not implicitly converted to some other value before comparison.

  • If the variable values are of different types, then the values are considered unequal.

  • If the variable is of the same type, is not numeric, and has the same value, they are considered equal.(null===null)

Lastly, If both variable values are numbers, they are considered equal if both are not NaN (Not a Number) and are the same value.

Some Examples:-

 console.log(true === true);//true
 console.log(4=== 4);//true
 console.log(2==="2");//false

🎄Difference between == and === operator🎄

Sr.no\==\===
1Compares two operandsCompares two operands
2returns true if operands have the same data type and same value, and returns false if the values differ.returns true only if operands are of the same data type and same value, otherwise returns false
3In case both operands are of different data types, it performs type conversion of one operand to make the data types of the operands the same.In case both operands are of different data types, it doesn't perform type conversion of the operands.
4Also known as loose equalityAlso known as strict equality

Conclusion

I explain hear what is == and === operators in javascript and what is the difference between them I hope that this article is useful to you, to help understand the topic better. If you have any questions, you can leave them in the comments section below.

Happy Coding.