Design & Download
Your JS Library
in Seconds!!!

Snippet: isNumeric() - Check If A String Can Be A Number

Written by Christopher West (cwest) on July 05, 2016.
Determines if a string can be represented as a finite number.
function isNumeric(value) {
  return  typeOf(value, 'String') && !value != isFinite(Number(value));
}

isNumeric(...) API Documentation

Determines if a string can be represented as a finite number.

Parameters

  1. value {string}:
    The string to be tested to determine if it can be represented as a finite number.

Returns

true is returned if value is a non-empty string that can be represented as a finite number. Otherwise false is returned.

Example

console.log(YourJS.isNumeric('1.234'));  // -> true
console.log(YourJS.isNumeric('-1e29'));  // -> true
console.log(YourJS.isNumeric('1e2989'));  // -> false
console.log(YourJS.isNumeric(1.234));  // -> false
console.log(YourJS.isNumeric('1.d'));  // -> false
console.log(YourJS.isNumeric(''));  // -> false