Curriculum

Curriculum

Avoid Mixing Numeric Text and Numbers

Scenario

A booking form sends freeSeats as text "2" instead of integer 2.

Selected choicePick a choice below
Result—

Try it yourself

Choose each expression to see how mixing strings and numbers breaks calculations.

Reveal the explanation
  1. "2" + 1 in JavaScript

    Produces text "21" (concatenation) instead of numeric 3.

  2. "10" < "2" (alphabetical text comparison)

    true! Text compares character by character ("1" before "2"), not numerically.

  3. Integer 2 + 1 and 10 < 2 after validating integer type

    Produces 3 and false predictably across Python, JavaScript, and Go.

Validate or convert form text into numbers once at the boundary, and keep internal calculations strictly numeric.

Review first

Your turn

Explain why "60" === 60 is false in strict comparison and write one test case that catches string score input.

Common mistake Relying on loose coercion so "60" silently passes in one language and crashes in Python or Go.

Check your work

Input score = "60" -> rejected with INVALID_SCORE (not silently coerced).

Quick check

Why is "10" < "2" true when comparing strings?
Show the answer and reasoning

Correct answer Strings compare character by character, and "1" comes before "2"

Lexicographical text comparison checks the first characters ("1" vs "2"), not numeric magnitude.