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.
console.log(/\d/.test("room 101"));
console.log(Number.isFinite(Number("room 101".trim())));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.
for (const v of ["123", "abc", "", " ", "123abc"]) {
console.log(JSON.stringify(v), isNaN(v));
}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.
for (const v of ["123", "123abc", "", " ", "Infinity"]) {
const n = Number(v);
console.log(JSON.stringify(v), n);
}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.
console.log(parseInt("123abc"));
console.log(parseFloat("123.45abc"));You should see two lines: 123, then 123.45.
Recommended — finite numeric string (js check if string is number)
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.
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));
}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.
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));
}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.
function isDecimalString(str) {
return /^-?\d+(\.\d+)?$/.test(str);
}
for (const s of ["123", "-123.45", "abc", "1e3", ""]) {
console.log(JSON.stringify(s), isDecimalString(s));
}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.
console.log(typeof "123");
console.log(typeof Number("123"));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
isNaNalone for check if number javascript; preferNumber+Number.isFiniteaftertrimand empty checks. - Remember
parseInt/parseFloatonly validate prefixes for check if string is number javascript. - Use
Number.isIntegerwhen 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.
- MDN:
Number - MDN:
Number.isFinite() - MDN:
Number.isInteger() - MDN:
Number.isNaN() - MDN:
isNaN() - MDN:
parseInt() - MDN:
parseFloat()
