Identically Equal and Not Identically Equal
The identically equal operator is ===. The not identically equal operator is !==.
The identically equal and not identically equal operators do not convert operands before testing for equality.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var result1 = ("2" == 2);
document.writeln(result1);//true - equal because of conversion
var result2 = ("2" === 2);
document.writeln(result2);//false - not equal because different data types
</script>
</head>
<body>
</body>
</html>
The following code uses the identically equal in an if statement
<!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>
The following code shows how to use the not identically equal operator.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var result1 = ("5" != 5);
document.writeln(result1);//false - equal because of conversion
var result2 = ("5" !== 5);
document.writeln(result2);//true - not equal because different data types
</script>
</head>
<body>
</body>
</html>
null and undefined are similar values:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var result = (null == undefined);
document.writeln(result);//true
</script>
</head>
<body>
</body>
</html>
null and undefined are not the same type.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var result = (null === undefined);
document.writeln(result);//false
</script>
</head>
<body>
</body>
</html>
Performing Equality and Identity Tests on Objects
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
var myData1 = {
name : "JavaScript",
weather : "Good",
};
var myData2 = {
name : "JavaScript",
weather : "Good",
};
var myData3 = myData2;
var test1 = myData1 == myData2;
var test2 = myData2 == myData3;
var test3 = myData1 === myData2;
var test4 = myData2 === myData3;
document.writeln("Test 1: " + test1 + " Test 2: " + test2);
document.writeln("Test 3: " + test3 + " Test 4: " + test4);
</script>
</body>
</html>
Performing Equality and Identity Tests on Primitives
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
var myData1 = 5;
var myData2 = "5";
var myData3 = myData2;
var test1 = myData1 == myData2;
var test2 = myData2 == myData3;
var test3 = myData1 === myData2;
var test4 = myData2 === myData3;
document.writeln("Test 1: " + test1 + " Test 2: " + test2);
document.writeln("Test 3: " + test3 + " Test 4: " + test4);
</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