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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book