Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Comparison Operators

The Four Comparisons

LyknJSOperation
(< a b)a < bLess than
(> a b)a > bGreater than
(<= a b)a <= bLess than or equal
(>= a b)a >= bGreater than or equal

These work on numbers (numeric comparison) and strings (lexicographic comparison, by UTF-16 code unit). Comparing values of different types triggers JavaScript’s coercion rules — another reason to type your function parameters.

(bind eligible (>= age 18))
(bind first-alphabetically (< name-a name-b))
const eligible = age >= 18;
const firstAlphabetically = nameA < nameB;

Equality

Equality was covered in Chapter 5 and doesn’t change here: (= a b) compiles to a === b. (js:eq a b) compiles to a == b. Use the first one. Always.