JavaScript – Equal-to (==)

JavaScript Equal-to (==) Comparison Operator is used to check if two values are equal. Equal-to operator returns a boolean value. The return value is true if the two values are equal, else, the return vale is false.

Equal-to Operator Symbol

The symbol used for Equal-to Operator is ==.

ADVERTISEMENT

Syntax

The syntax to use Equal-to Operator with operands is

operand1 == operand2

Each operand can be a value or a variable.

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

Equal-to operator does not check if the type of values being compared is same. For example, the expression 5 == '5' returns true in JavaScript.

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 using Equal-to Operator.

index.html

Since equal-to operator does not check the type of the operands, but the values alone, let us check if 5 is equal to '5'.

index.html

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

index.html

Conclusion

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