Skip to the workspace
Pebellslearn by building
Data Types  ›  Lesson

Text and Number Boundaries

string 20 min

javascript / Text and Number Boundaries

Explore each scene at your own pace.

01 · Text and Number Boundaries

Text and Number Boundaries

Text and Number Boundaries

JavaScript strings are immutable sequences of UTF-16 code units, and Number values use binary floating-point representation. Validate meaning at input boundaries instead of assuming displayed characters and numeric text already have the types you need.

A form supplies text. Decide what it means before calculating, and distinguish a formatted label from the number behind it.

Start at a real input boundary

An article editor asks for a nonnegative whole number of reading minutes. An HTML text input supplies a string, including when it displays digits. Converting that string is one step; checking the accepted range and shape is another. Number('') produces zero, which is usually the wrong interpretation of an empty required field.

Walk through the modelText and Number BoundariesAuthored snapshots of the example; use Run below to test the code.
raw" 12 "
typestring
An input control gives text, even when it contains digits.
1 of 3
javascript

The validator trims surrounding spaces, rejects the empty string, requires ASCII decimal digits, converts, and then requires a safe integer. It deliberately rejects decimals, signs, exponents and trailing text. Those are product rules for this field, not universal rules for every numeric input. If a product accepts decimals or localized digits, write a different contract and test it.

Precision and formatting

0.1 + 0.2 is not exactly 0.3 in binary floating point. A formatting method such as toFixed(2) returns a string; it does not improve the underlying number's exactness. For exact integer counts, stay within Number's safe-integer range or choose BigInt when appropriate. Monetary calculations need an explicit representation and rounding policy; a call to toFixed alone is not that policy.

Number.isNaN tests the actual NaN value without the coercion done by the global isNaN function. Number.isFinite rejects NaN and infinities. A finite value can still be outside the field's allowed range. Do not use a truthiness test to reject zero when zero is valid.

Characters and strings

'A😀'.length is 3 because the emoji uses two UTF-16 code units. [...'A😀'] iterates Unicode code points and has length 2. Neither rule universally counts user-perceived characters: a displayed character can contain several code points. Choose the measurement your product needs rather than assuming length always means visible letters. String methods return new strings; they do not mutate the original primitive.

Independent check

Implement the validator exactly as stated. Test empty input, zero, a padded integer, a decimal, a suffix and an integer outside the safe range. Explain why parseInt('12px', 10) can be useful for extracting a prefix but is not sufficient validation for a field that accepts only complete integer text.

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.