Horje
Difference Between == & === in JavaScript

In JavaScript, equality operators like double equals (==) and triple equals (===) are used to compare two values. But both operators do different jobs. Double equals (==) will try to convert the values to the same data type and then try to compare them. But triple equals (===) strictly compares the value and the datatype.

Two Types

  • Double equality (==)
  • Triple equality (===)

Example 1: Double Equality (==)

In this example, we are trying to compare a string and a number.

  • Assigning the value “10” to a variable called string.
  • Assigning the value 10 to a variable called number.
  • The third statement compares the string and number and prints the result on the console. Here we are using double equals (==) so it checks the inner value rather than the datatype.

Javascript

let string = "10";
let number = 10;
  
console.log(string == number);

Output

true

The output is true, because they have different data type but same value.

Example 2: Triple Equality (===)

In this example, we are trying to compare a string and a number.

  • Assigning the value “20” to a variable called string.
  • Assigning the value 20 to a variable called a number.
  • The third statement compares the string and number and prints the result on the console. Here we are using triple equals (===) so it checks the inner value and the datatype.

Javascript

let string = "20";
let number = 20;
  
console.log(string === num);

Output:

false



Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
How to check NoneType in Python How to check NoneType in Python
How To Convert Generator Object To Dictionary In Python How To Convert Generator Object To Dictionary In Python
Different Appium Scroll Strategies Different Appium Scroll Strategies
Remove URLs from string in Python Remove URLs from string in Python
What is Math in JavaScript? What is Math in JavaScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16