Skip to content

Comparison operators

Operators explained

Here are the most common comparison operators in javascript:

UsageDescription
a > bIs a bigger than b?
a >= bIs a bigger than or equal to b?
a < bIs a smaller than b?
a <= bIs a smaller than or equal to b?
a === bIs a strictly equal to b? (same value and type)
a == bIs a loosely equal to b ?

TIP

As a general rule, prefer === unless you know you need ==.

Examples

Using inequalities

js
const minimumAge = 18
let userAge = 21
console.log(userAge >= minimumAge)
console
true

Using equality operators

js
const bookCondition = 'good'

console.log(bookCondition === 'poor')
console.log(bookCondition === 'good')
console
false
true

Strict and loose equality

As we mentioned, == and === have different behaviour.

js
let userId = '123'
let inputId = 123

// loose equality check
console.log(userId == inputId)

// strict equality check
console.log(userId === inputID)
console
true
false