auto-type conversion

difference between == and === in JavaScript

In this post, I am going to share the very interesting double equals "==" and triple equals "===". It is very confusing topic and most of the time peoples are confused.

The details example as given below.

The double equals (==) are used for check only value of its variables but triple equals (===) are used for check value and type as well of its variables.

1.  The double equal “==” is an auto-type conversion and it checks only value not type.

2.  The triple equal “===” is not auto-type conversion and it check value and type both.

For example,
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Difference between == and === in JavaScript</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
  <script>
    // alert(0 == false); // return true, because both are same type.
    // alert(0 === false); // return false, because both are of a different type.
    // alert(1 == "1"); // return true, automatic type conversion for value only.
    // alert(1 === "1"); // return false, because both are of a different type.
    // alert(null == undefined); // return true.
    // alert(null === undefined); // return false.
    //alert('0' == false); // return true.
    // alert('0' === false); // return false.
    // alert(1=== parseInt("1")); // return true.


    console.log(0 == false); // return true, because both are same type.
    console.log(0 === false); // return false, because both are of a different type.
    console.log(1 == "1"); // return true, automatic type conversion for value only.
    console.log(1 === "1"); // return false, because both are of a different type.
    console.log(null == undefined); // return true.
    console.log(null === undefined); // return false.
    console.log('0' == false); // return true.
    console.log('0' === false); // return false.
    console.log(1 === parseInt("1")); // return true.
  </script>
</head>

<body>
  <h3>Difference between == and === in JavaScript</h3>
</body>

</html>

Example and Result as below


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.