Comparison Operators

A comparison operator compares the values of its two operands and returns a Boolean (true or false). Six comparison operators exist, and you probably learned about all of them but one in elementary school.

1. “Equals” checks that the two operands are the same. In most languages, the operator is ==. In JavaScript, it is best to use ===, for reasons that are explained in the section on coercion. So 'abc' === 123 returns false and 42 === 42 returns true.

2. “Not equals” checks that the two operands are not the same. Whatever the equals operator would return for two operands, not equals will return the opposite. In many languages, the not equals operator is !=, but again, JavaScript is different and uses !==. Now 'abc' !== 123 ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.