OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Numbers and explicit conversion

Check numeric input without accepting a partially parsed value by accident.

Lesson 4 of 25 · About 25 minutes · Beginner · English

By the end of this lesson

Require a finite safe integer in a stated range.

Before you start

  • Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
  • Complete lesson 3 or be comfortable with its examples.

Three words to know

NaN
A special Number value representing an invalid numeric result.
Safe integer
An integer exactly distinguishable within the Number safe range.
Range rule
The smallest and largest values your application accepts.

1. Conversion and grammar are separate

Number converts a complete numeric string but accepts forms such as whitespace and exponent notation. Number("") is zero. parseInt can accept a numeric prefix and ignore later characters. Choose whether you want a numeric value or an exact text grammar before using either.

Unexpected conversionsJAVASCRIPT · Node.js · save as lesson.mjs
console.log(Number(""));
console.log(Number("1e2"));
console.log(Number.isNaN(Number("12pages")));
console.log(Number.parseInt("12pages", 10));
What you should see · example output
0
100
true
12

2. State a strict count rule

This example accepts only ASCII decimal digits, then checks the numeric range. The negative character test rejects any character outside 0–9, including line separators. It avoids relying on a $ anchor to mean absolute end-of-string. Leading zeroes are accepted deliberately.

function parsePages(raw) defines a named rule: raw receives the supplied text, return gives back the checked number, and throw stops the rule with an error. Lesson 11 develops functions in detail; for now follow those three parts of the complete example.

Validate a small page countJAVASCRIPT · Node.js · save as lesson.mjs
function parsePages(raw) {
  if (typeof raw !== "string" || raw.length === 0 || /[^0-9]/u.test(raw)) {
    throw new Error("Use decimal digits only");
  }
  const value = Number(raw);
  if (!Number.isSafeInteger(value) || value < 1 || value > 50) {
    throw new Error("Choose 1 to 50 pages");
  }
  return value;
}
console.log(parsePages("12"));
What you should see · example output
12

3. Approximate numbers have limits

Number uses binary floating-point. Do not assume decimal fractions are exact or that every large integer is representable. Whole cents can be convenient for a small money example but are not a complete accounting policy. Format for display separately from the arithmetic rule.

Make rounding visibleJAVASCRIPT · Node.js · save as lesson.mjs
console.log((0.1 + 0.2).toFixed(2));
console.log(Number.isSafeInteger(9007199254740992));
What you should see · example output
0.30
false

YOUR TURN

A small check before you continue

Would the count validator accept "12pages", " 12" or "0"?

Show a suggested answer

The first two fail the digit grammar. "0" meets the grammar but fails the 1-to-50 range. The validator also rejects trailing Unicode line separators.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.