Equal and Not Equal
The equal operator in JavaScript is ==. The not-equal operator is !=.
Rules for the equal and not-equal operators:
Expression | Value |
---|---|
null == undefined | true |
null == any other value | false |
undefined == any other value | false |
NaN == any other value | false |
NaN != NaN | true |
false == 0 | true (false converts to 0, true converts to 1) |
true == 1 | true (false converts to 0, true converts to 1) |
true == 2 | false (false converts to 0, true converts to 1) |
undefined == 0 | false |
null == 0 | false |
"5" == 5 | true |
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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator