Introduction:
Here I will explain difference between == and === in JavaScript with example or what is the difference between == and === in jQuery with example. Generally JavaScript will provide different type of operators those are strict equality (===) and type converting equality (==). These operators help us to understand difference between == and === in JavaScript or jQuery.
Description:
In previous articles I explained jQuery bootstrap autocomplete textbox from database example, jQuery slideup, slidedown and slidetoggle example, jQuery autocomplete with images in asp.net, jQuery hide div if we click outside of it example and many articles relating to css, AngularJS, jQuery, JavaScript and asp.net. Now I will explain difference between == and === in JavaScript or jQuery with example.
In previous articles I explained jQuery bootstrap autocomplete textbox from database example, jQuery slideup, slidedown and slidetoggle example, jQuery autocomplete with images in asp.net, jQuery hide div if we click outside of it example and many articles relating to css, AngularJS, jQuery, JavaScript and asp.net. Now I will explain difference between == and === in JavaScript or jQuery with example.
Generally JavaScript
will provide different type of operators those are strict equality and type
converting equality.
Strict equality (===) means values
which we are comparing must have same type that means "2" not equal
to 2 ("2"===2 it will return false).
Type converting equality (==) means
automatically it will covert variable to value either it's string or number that
means "2" is equal to 2 ("2" == 2 it will return true)
Here double equal (==) is an auto type converting equality and three equal (===) is strict equality
operator that means it will not covert values automatically.
Here we will learn with simple
examples
Example1
1=="1" // it will return true because here string will
converted as number
1 === "1" // it will return false because here 1 is number and
"1" is string
|
Example2
0 == false //
it will return true because here false is equivalent of 0
0 === false // it will return false because both are different operands
|
If
you want to check it in complete example you need to write the code like as
shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>difference between ==
and === in javascript</title>
<script type="text/javascript">
function Checkdiff() {
var tstval = "2";
if (tstval == 2)
document.write("== Condition
reutrns true <br/>")
else
document.write("== Condition
reutrns false <br/>")
if (tstval === 2)
document.write("=== Condition
reutrns true <br/>")
else
document.write("=== Condition
reutrns false <br/>")
}
</script>
</head>
<body>
<div>
<input type="button"
id="btncheck"
onclick="Checkdiff()"
value="Check
Difference" />
</div>
</body>
</html>
|
Live
Demo
Check following demo to check difference between == and === in jQuery
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
3 comments :
Very simple and good one. Thank you.
Good one..
Good one.
Note: Only a member of this blog may post a comment.