Equal and Not Equal

The equal operator in JavaScript is ==. The not-equal operator is !=.

Rules for the equal and not-equal operators:

ExpressionValue
null == undefinedtrue
null == any other valuefalse
undefined == any other valuefalse
NaN == any other valuefalse
NaN != NaNtrue
false == 0true (false converts to 0, true converts to 1)
true == 1true (false converts to 0, true converts to 1)
true == 2false (false converts to 0, true converts to 1)
undefined == 0false
null == 0false
"5" == 5true

If one operand is a string and the other is a number, the string is converted into a number. If only one of the operands is an object, the valueOf() is called on the object to get a primitive value. If both operands are objects and they are the same object, the equal operator is used.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
  <script type="text/javascript">
    var firstVal = 5;
    var secondVal = "5";
    if (firstVal == secondVal) {
      document.writeln("They are the same");
    } else {
      document.writeln("They are NOT the same");
    }
  </script>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Operators:
  1. JavaScript Operators
  2. Increment/Decrement Operators
  3. Increment/Decrement Operators for String, Boolean, Floating-point and Object
  4. Unary Plus and Minus
  5. Bitwise Not operator
  6. Bitwise AND
  7. Bitwise OR
  8. Bitwise XOR
  9. Left Shift
  10. Signed Right Shift
  11. Unsigned Right Shift
  12. Logical NOT
  13. Logical AND
  14. Logical OR
  15. Multiply
  16. Divide
  17. Modulus
  18. Add
  19. Subtract
  20. Relational Operators
  21. Equal and Not Equal
  22. Identically Equal and Not Identically Equal
  23. Conditional Operator
  24. Assignment Operators
  25. Comma Operator