Check if a string is a number in JavaScript

Learn how to reliably check if a string represents a number in JavaScript. Compare parseInt, Number, and Number.isFinite, avoid common pitfalls, and handle edge cases with clear code samples.

JavaScripting
JavaScripting Team
·5 min read

Understanding numeric strings in JavaScript

This section explains what it means for a string to represent a number in JavaScript and why you should care about reliable numeric checks. In real-world apps you often receive data as strings from user input, query params, or APIs. The goal is to determine whether such strings can be interpreted as finite numbers without triggering runtime errors or unexpected coercion. The phrase check if string is number javascript captures this common validation need. Below are practical examples and patterns you can adopt in your codebase.

JavaScript
function isNumericString(str) { if (typeof str !== 'string') return false; const trimmed = str.trim(); if (trimmed === '') return false; const num = Number(trimmed); return Number.isFinite(num); } console.log(isNumericString("123")); // true console.log(isNumericString(" 45.6 ")); // true console.log(isNumericString("12a3")); // false
JavaScript
console.log(isNumericString("")); // false console.log(isNumericString(" ")); // false console.log(isNumericString(null)); // false

Takeaway: Start with a strict finite-number check after trimming to avoid misleading results from coercion.

Related Articles