python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions