Skip to the workspace
Pebellslearn by building
Expressions & Operators  ›  Lesson

Comparison and String Operators

Comparison Operators 12 min

javascript / Comparison and String Operators

Explore each scene at your own pace.

01 · Comparison and String Operators

Comparison and String Operators

Comparison and String Operators

Comparison operators test ordering or equality. For two strings, relational comparison uses lexicographic UTF-16 code-unit ordering.

Text order and numeric order answer different questions.

See the mechanism

Walk through the modelComparison and String OperatorsAuthored snapshots of the example; use Run below to test the code.
12 > 3true
Numbers compare by numeric magnitude.
1 of 3

Follow one example

javascript

The numeric comparison is true. With two strings, comparison starts at the first differing code unit: "1" precedes "3", so "12" > "3" is false. + joins two strings. Neither behavior sorts numeric input correctly unless you first decide whether the data is text or numbers.

Predict, change and check

Predict: Will "9" > "10" use numeric magnitude or the first differing character?

Test your explanation: Run that comparison beside 9 > 10. Write down the types next to the two results so the reason is visible.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Comparison and text joining need explicit inputs

Relational comparison can behave lexicographically for two strings and numerically for other primitive combinations. The displayed strings '10' and '2' therefore do not establish numeric order. Normalize inputs when the feature asks for numeric order rather than relying on a changing mixture of representations.

String concatenation builds new text and does not validate the data embedded in it. Template literals improve readability but do not make untrusted text safe as HTML or SQL. Keep plain text in textContent for DOM output and use the appropriate structured API at other boundaries.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.