Asked 7 years ago
6 Jan 2017
Views 1314
denyy

denyy posted

=== (triple equal to) vs == ( double equal to ) operator in JavaScript

where i should use == (double equal to) operator and === (triple equal to) operator ?
what is the difference between == (double equal to) operator and === (triple equal to) ?

i know (single equal to) = used as assignment operator . it assigns values , suppose

var mycode=10; 

simple assign 10 to mycode

and == (double equal to) is equality check operator means it check is both left and right side operands are equal or not
double equal check code ::

if(mycode==10){ alert(mycode)} 

it give alert of mycode's value if it equal to 10
so == (double equal) check, is it equal to or not , if do same check with triple equal to it behave same
triple equal check code ::

if(mycode===10){ alert(mycode)} 

still it give of mycode's value if it equal to 10

so it seems both == (double equal to) operator and === (triple equal to) operator had same behavior . so why one made extra operator === (triple equal to) operator, it had to some reason and math behind the === (triple equal to) operator , so please tell me what is the difference between == (double equal to) operator and === (triple equal to) ?
Phpworker

Phpworker
answered Nov 30 '-1 00:00

== operator check only values not type
=== operator check values and type also
jagdish

jagdish
answered Nov 30 '-1 00:00

=== operator check value and type , == operator check for only value .


var mycode="10";
if(mycode===10){
alert("i am 10 integer");
}
if(mycode==="10"){
alert("i am 10 string");
}

you will got seond alert
because mycode varable have 10 (string ) so if you check it with === 10 (integer) it will return false and === 10 (string) it will return true.


var mycode="10";
if(mycode==10){
alert("i am 10  , not matter integer or string . just value");
}
if(mycode=="10"){
alert(' i am 10  , not matter integer or string . just value');
}

you will get two alert
you will got both true in == check because value is only going to check == operator .

Post Answer