Skip to main content
Difference between == and === with Example-JavaScript

JavaScript-Difference between == and ===

This is one of the most frequently asked question in an interview when the interviewer tries to analyze your basic concepts of JavaScript.

JavaScript provides different types of operators. Here, we will be talking about strict equality and Type converting equality operators.

Difference between == and === with Example-JavaScript #difference #javascript Click To Tweet

Type converting equality (==)

It will automatically convert variable to value irrespective of data type, either it is string or number. For an example “2” will be equal to 2 ("2"== 2 will return true). So the double equal (==) is an auto type converting equality.

Strict equality (===)

It is strict equality operator i.e it will not convert the values automatically. For an example “2” will not be equal to 2 ("2"=== 2 will return false)

Below is the demonstration with simple examples:

Demo 1

Learn Web Development!1=="1"
will return true because here “1” (string) will converted as 1 (number)
1 === "1"
will return false because here 1 is number and “1” is string

Demo 2

0 == false
it will return true because here false is equivalent of 0
0 === false
it will return false because both are different operands

Difference between == and === with an example in JavaScript

<!DOCTYPE HTML>
<html>
<head>
	<title>Difference between == and === in JavaScript - DotNetCrunch</title>
	<script type="text/javascript">
		function CheckDifference() {
			var val = "2";
			document.write("Value for a variable is : " +val +"<br/>"); 

			if (val == 2)
				document.write("== returns True <br/>")
			else
				document.write("== returns False <br/>")

			if (val === 2)
				document.write("=== returns True <br/>")
			else
				document.write("=== returns False <br/>")
		}
	</script>
</head>
<body>
	<h2>JavaScript - Difference between == and ===</h2>
	<hr/>
	<br/>
	<div id="div1">
		<input type="button" id="btnCheckDifference" onclick="CheckDifference()" 
		value="Click to Check Difference" />
	</div>
</body>
</html>

Hope you enjoyed this post and gained knowledge about the difference between == and === in JavaScript.

What do you think?

Dear Readers,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Coding!

Udemy

DotNetCrunch

DotNetCrunch is a popular blog for latest Microsoft® technologies which is aimed for beginners and intermediate level professionals.