In this article, We will show you the difference between == vs === in JavaScript. Both == and === are used for the comparison. Let’s see how both are different.
== vs === in JavaScript
== (Double Equals) | === (Triple Equals) |
---|---|
It is used to compare the variables or values | It is also used to compare the variables/values |
Doble equals do not check the data type of variable/value while the comparison | Triple Equals checks the data type of a variable while the comparison |
Use this when you are unsure about the data type of variable/values | Use this when you sure about the data type of variable/values and want strict comparison. |
'1' == 1 This will return true because double equals don’t check the data type. | '1' === 1 This will return false because triple equals compare the data type too. Simply string is not a number |
0 == false It will return true because double equals convert the 0 to false and then compared | 0 === false It will return false because they are of a different type |
7 == "7" // true | 7 === "7" // false |
7 == 7 // true | 7 === 7 // true |
That’s it for now. We hope this article helped you to learn the difference between == vs === in JavaScript.
Additionally, read our guide:
- jQuery Form Submit With Examples
- Check If Array Is Empty Or Undefined In JavaScript
- How To Detect IE Browser In JavaScript
- AJAX PHP Post Request With Example
- How To Declare A Global Variable in Vue
- How To Remove A Specific Item From An Array In JavaScript
- How To Check Array Contains A Value In JavaScript
- How To Merge Objects In Vue
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!