Difference between != and !== in javascript

Hello friends, In this article, we will learn Type coercion, explicit type coercion and implicit type coercion in javascript.

Type coercion is the process of converting value from one type to another for example string to number, number to boolean, object to boolean etc.

Type coercion can be of two types explicit type coercion and implicit type coercion.

Explicit Type Coercion:

When a we converts between types like Number(value) , parseInt(value) , parseFloat(value) etc. it’s called explicit type coercion or type casting.

Implicit Type Coercion:

We knows JavaScript is a weakly-typed language, values can also be converted between different types automatically, and it is called implicit type coercion.

Example:

String(4512) // explicit 4512 + ''    // implicit Number('123')   // explicit +'123'          // implicit 123 != '456'    // implicit 4 > '5'         // implicit 5/null          // implicit true | 0        // implicit

More Implicit Type Coercion:

"number" + 16 + 3        // 'number163' 16 + 4 + "number"        // '20number' true + false             // 1 12 / "6"                 // 2 [1] > null               // true 'true' == true           // false false == 'false'         // false null == ''               // false !!"false" == !!"true"    // true ['x'] == 'x'             // true  [1,2,3] == [1,2,3]       // false "SWC" + + "bar"          // 'SWCNaN' [] + null + 1            // 'null1' {}+[]+{}+[1]             // '0[object Object]1' !+[]+[]+![]              // 'truefalse' new Date(0) - 0          // 0 new Date(0) + 0          // 'Thu Jan 01 1970 02:00:00(EET)0'

"!=" operator is known as type coercion operator and "!==" is known as strictly equality operator.  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 false n !== s returns true

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 false n !== b returns true

Explanation:

Since n is of numeric type. and b is boolean. "!=" operator converts both variable into same type then compares returns false. 

while "!==" operator first compares their types then their values returns true.

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