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.
" 12 "stringThe 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.