How to use Identically Equal and Not Identically Equal in Javascript

Description

The identically equal operator is === and returns true only if the operands are equal without conversion.

The not identically equal operator is !== and returns true only if the operands are not equal without conversion.

Example


var result1 = ("5" == 5);    //true - equal because of conversion
console.log(result1);/*from   w w  w .  j a  v  a 2  s  .  c  o  m*/
var result2 = ("5" === 5);   //false - not equal because different data types
console.log(result2);

result1 = ("5" != 5);    //false - equal because of conversion
console.log(result1);
result2 = ("5" !== 5);   //true - not equal because different data types
console.log(result2);

The code above generates the following result.

Note

null === undefined is false because they are not the same type.

It is recommended to use identically equal and not identically equal to maintain data type integrity.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions