JavaScript check if string is number (Number.isFinite, isNaN, parseInt)

Validate numeric strings in JavaScript: whole-token checks with trim + Number.isFinite, global isNaN pitfalls, parseInt prefix behavior, integers, strict regex decimals, and digits-in-text vs fully numeric. Runnable Node snippets with short output notes.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

JavaScript check if string is number (Number.isFinite, isNaN, parseInt)

javascript check if string is number usually means the entire string (after trimming) parses as one finite numeric value—a common js check if string is number task for forms and APIs. That differs from javascript check if string contains numbers (any digit inside text). Trimming overlaps with check if string contains spaces / only whitespace; splitting tokens appears in convert string to array.

Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.


Quick reference

Whole-string validation almost always needs trim, reject empty, then Number.isFinite(Number(t)); parseInt only proves a prefix.

Goal Approach
Whole string is a finite number trim, non-empty, then Number.isFinite(Number(t))
Integer numeric string Same, plus Number.isInteger(n)
Any digit in text /\d/.test(str) (or Unicode-aware checks if needed)
Exact ASCII decimal shape Anchored regex like /^-?\d+(\.\d+)?$/

Contains digits vs whole string is numeric

The first test answers javascript check if string contains numbers; the second answers whether the full token is one number—room 101 has digits but is not numeric as a whole.

javascript
console.log(/\d/.test("room 101"));
console.log(Number.isFinite(Number("room 101".trim())));
Output

You should see two lines: true, then false.


Global isNaN — coercion surprises (check if number javascript)

Global isNaN coerces before testing, so isNaN('') and isNaN(' ') are false because the string becomes 0. For check if number javascript validation, use an explicit Number(...) (or parseFloat) plus Number.isFinite.

javascript
for (const v of ["123", "abc", "", " ", "123abc"]) {
  console.log(JSON.stringify(v), isNaN(v));
}
Output

You should see five lines: "123" false, "abc" true, "" false, " " false, "123abc" true.


Number() — full-string parse (javascript isnumeric)

Number parses the whole string (unlike parseInt prefix rules). Empty and whitespace-only strings become 0, and "Infinity" becomes Infinity—so pair with trim, reject empty input, and use Number.isFinite when you mean a finite javascript isnumeric check.

javascript
for (const v of ["123", "123abc", "", " ", "Infinity"]) {
  const n = Number(v);
  console.log(JSON.stringify(v), n);
}
Output

You should see five lines showing each string and its numeric conversion (NaN for "123abc", 0 for "" and " ", Infinity for "Infinity").


parseInt and parseFloat — prefix parsing (js check if number)

parseInt and parseFloat return a numeric prefix and stop at the first invalid character—they do not prove the whole string was numeric, only that a numeric prefix exists for js check if number style scans.

javascript
console.log(parseInt("123abc"));
console.log(parseFloat("123.45abc"));
Output

You should see two lines: 123, then 123.45.


This helper is the usual js check if string is number pattern: require typeof str === "string", trim, reject empty, then Number.isFinite(Number(t)). It accepts normal decimal and scientific forms that Number allows. If you must reject hexadecimal strings such as "0x10", add an explicit rule because Number("0x10") is 16.

javascript
function isNumericString(str) {
  if (typeof str !== "string") return false;
  const t = str.trim();
  if (t === "") return false;
  const n = Number(t);
  return Number.isFinite(n);
}

for (const s of ["123", "-1.5", "1e3", "", "   ", "12a"]) {
  console.log(JSON.stringify(s), isNumericString(s));
}
Output

You should see six boolean results: "123" true, "-1.5" true, "1e3" true, "" false, " " false, "12a" false.


Integer-only string (javascript isnumber for integers)

Add Number.isInteger(n) when javascript isnumber must mean integers only. Without the empty guard, Number("") is 0, which is an integer—a common pitfall.

javascript
function isIntegerString(str) {
  if (typeof str !== "string") return false;
  const t = str.trim();
  if (t === "") return false;
  const n = Number(t);
  return Number.isFinite(n) && Number.isInteger(n);
}

for (const s of ["123", "123.45", "", "   ", "1e3"]) {
  console.log(JSON.stringify(s), isIntegerString(s));
}
Output

You should see five lines: "123" true, "123.45" false, "" false, " " false, "1e3" true.


Strict decimal pattern (no scientific notation) — regex

Use an anchored pattern when check if string is number js must allow only ASCII digits and one optional decimal point—not scientific notation like 1e3.

javascript
function isDecimalString(str) {
  return /^-?\d+(\.\d+)?$/.test(str);
}

for (const s of ["123", "-123.45", "abc", "1e3", ""]) {
  console.log(JSON.stringify(s), isDecimalString(s));
}
Output

You should see five lines ending with "1e3" false and "" false.


typeof — is number javascript on the value itself

typeof on a string literal is always "string"; after conversion, typeof Number("123") is "number". Use this when distinguishing string inputs from numeric results, not as the sole isnumeric javascript test on raw form fields.

javascript
console.log(typeof "123");
console.log(typeof Number("123"));
Output

You should see two lines: string, then number.


Summary

Separate “contains a digit” from “entire token is numeric”; prefer Number + Number.isFinite after trim and empty checks, and avoid parseInt alone for strict validation.

  • Distinguish javascript check if string contains numbers from whole-string numeric validation.
  • Avoid global isNaN alone for check if number javascript; prefer Number + Number.isFinite after trim and empty checks.
  • Remember parseInt/parseFloat only validate prefixes for check if string is number javascript.
  • Use Number.isInteger when javascript isnumber must mean integers only, with an empty-string guard.
  • Use a strict regex when scientific notation and loose shapes must fail.

References

MDN references for Number, Number.isFinite, Number.isInteger, Number.isNaN, global isNaN, parseInt, and parseFloat—APIs behind this javascript check if string is number guide.


Frequently Asked Questions

1. What is the safest pattern for javascript check if string is number (finite)?

After confirming typeof value === 'string', use const t = value.trim(); if (t === '') return false; return Number.isFinite(Number(t));. That rejects empty and whitespace-only strings, Infinity, NaN, and non-numeric text.

2. Why is global isNaN alone a bad js check if string is number?

isNaN coerces its argument to a number first. For example isNaN('') and isNaN(' ') are false because the string becomes 0. Use Number(...) plus Number.isFinite or Number.isNaN after an explicit conversion you control.

3. javascript check if string contains numbers vs the whole string being a number?

Contains any digit is different from the entire string parsing as one number. Use /\d/.test(str) for contains digit; use Number.isFinite(Number(str.trim())) when the whole token must be numeric.

4. Why does parseInt('123abc') look valid for check if string is number javascript?

parseInt and parseFloat stop at the first invalid character and return the prefix as a number. For validation of the full string, prefer Number(str) or compare the parsed value back to the original string intentionally.

5. How do I restrict to integers only (javascript string is number integer)?

After trim and non-empty checks, require Number.isFinite(n) && Number.isInteger(n) where n = Number(t). Remember scientific notation like 1e3 is still an integer value (1000).

6. Does Number('Infinity') pass a finite check?

No. Number('Infinity') is Infinity and Number.isFinite(Infinity) is false. Global isNaN('Infinity') is false, so do not rely on isNaN alone for validation.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …