I hope you are doing.
This article demonstrates what are the differences between
=, == and === in JavaScript.
= (Assignment Operator)- Basically, using this operator we will assign value to some variable.
Example: var val1 = 10;
== and === (Relational Operators):
- To compare the values of two variable we will use these operators.
- == is used to compare irrespective of type of value.
- === is used to compare value along with its type.
To know more clearly, please copy and paste following code
in notepad and save it as html file. Then open the page with browser.
<!DOCTYPE html>
<HTML>
<head>
<title>
Comparision.
</title>
<!--Following
represents the JavaScript-->
<script type="text/javascript">
function Compare1() {
var val1 = 10; // assigning the value to variable val1
var val2 = "10"; // assigning the value to variable
val2
// use of ==
alert(val1 == val2); // comparing the 2 variable using ==
// returns true
}
function Compare2() {
var val1 = 10; // assigning the value to variable val1
var val2 = "10"; // assigning the value to variable
val2
// use of ===
alert(val1 === val2); // comparing the 2 variable using ===
// returns false
}
</script>
<!--Following
represents Cascading Style Sheets (CSS)-->
<style type="text/css">
body {
font-family: Arial, Helvetica, sans-serif;
}
.btn1 {
width: 250px;
height: 35px;
margin: 4px;
background-color: cadetblue;
color: white border-radius: 4px;
border-radius: 4px;
border: 1px solid silver;
font-weight: bold;
font-size: 18px;
}
.btn2 {
width: 250px;
height: 35px;
margin: 4px;
background-color: chocolate;
color: white;
border-radius: 4px;
border: 1px solid silver;
font-weight: bold;
font-size: 18px;
}
.PageHeading {
color: green;
}
</style>
</head>
<body>
<h2 class="PageHeading"><i>The difference among =, == and ===
in JavaScript</i></h2>
<hr>
<br>
<button onclick="Compare1()" class="btn1">Compare using
==</button>
<br>
<br>
<button onclick="Compare2()" class="btn2">Compare using
===</button>
</body>
</HTML>
Output:
The output will be like below.
After clicking the “Compare using ==” button, the
following is the result
Similarly, after clicking the “Compare using ===”
button, the following is the result
If you have any doubts, please raise in comments section.
Thanks for reading the article.
No comments:
Post a Comment