• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Math Related / Check if the given value is a number

Check if the given value is a number

Check if the given value is a number

Use parseFloat() to try to convert n to a number.

Use !Number.isNaN() to check if num is a number.

Use Number.isFinite() to check if num is finite.

Use Number() and the loose equality operator (==) to check if the coercion holds.

const validateNumber = n => {
  const num = parseFloat(n);
  return !Number.isNaN(num) && Number.isFinite(num) && Number(n) == n;
}

// Examples
validateNumber('10'); // true
validateNumber('a'); // false

Source

https://www.30secondsofcode.org/js/s/validate-number

Math Related

Related Snippets:

  • Generating random numbers with JavaScript
  • Round a number to a specified amount of digits
  • Check if the given number is even
  • Get the largest number from a set of numbers

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers