Difference between == and === operator in javascript

"==" operator is known as type coercion operator and "===" is known as strictly equality operator. Because JavaScript support both strict equality and type-converting equality, 

it's important to know which operator is used for which operation.

"==" operator compares variable by their values. Where as "===" operator not only checks the value of variable moreover it compares variable by making their type correction.

Following examples will make it simple to understand.

var n = 12; var s = "12"; document.write("n == s returns " + (n==s) +"<br />"); document.write("n === s returns " + (n===s)); 

OUTPUT: n == s returns true n === s returns false

Explanation: 

Since n is of numeric type. and s is string. "==" operator converts both variable into same type then compares.  while "===" operator first compares their types then their values.

Example:

var n = 0; var b = false; document.write("n == b returns " + (n==b) +"<br />"); document.write("n === b returns " + (n===b));

OUTPUT: n == b returns true n === b returns false

Explanation: 

Since n is of numeric type. and b is boolean. "==" operator converts both variable into same type then compares returns true.  while "===" operator first compares their types then their values returns false.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories