JavaScript – Equal Value Equal Type (===)

JavaScript Equal Value Equal Type (===) Comparison Operator is used to check if two values are equal both in value and type. Equal Value Equal Type operator returns a boolean value. The return value is true if the two values are equal both in value and datatype, else, the return vale is false.

Equal Value Equal Type Operator Symbol

The symbol used for Equal Value Equal Type Operator is ===.

Syntax

The syntax to use Equal Value Equal Type Operator with operands is

</>
Copy
operand1 === operand2

Each operand can be a value or a variable.

Since Equal Value Equal Type operator returns a boolean value, the above expression can be used as a condition in If-statement.

</>
Copy
if (operand1 === operand2) {
    //code
}

Examples

In the following example, we take two values in variables: x and y; and check if the values in x and y are equal in value and type using Equal Value Equal Type Operator.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var x = 4;
        var y = 4;
        var result = (x === y);
        document.getElementById('output').innerHTML += 'Are x and y are equal in value and type?  ' + result;
    </script>
</body>
</html>

Now, let us take values in x and y, such that they are equal in value, but not in type.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var x = 4;
        var y = '4';
        var result = (x === y);
        document.getElementById('output').innerHTML += 'Are x and y are equal in value and type?  ' + result;
    </script>
</body>
</html>

In the following example, let us use the Equal Value Equal Type operator in the If statement’s condition.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var x = 'apple';
        var y = 'apple';
        if (x === y) {
            document.getElementById('output').innerHTML += 'x and y are equal.';
        } else {
            document.getElementById('output').innerHTML += 'x and y are not equal.';
        }
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned about Equal Value Equal Type Comparison Operator, its syntax, and usage with examples.