Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Number Validation in JavaScript
Next article icon

Number Validation in JavaScript

Last Updated : 03 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are the various methods to validate numbers in JavaScript

1. Check if a Value is a Number

Use typeof and isNaN() to determine if a value is a valid number.

JavaScript
const isNum = n => 	typeof n === 'number' && !isNaN(n);  console.log(isNum(42));  console.log(isNum('42')); console.log(isNum(NaN)); 

In this example

  • typeof n === 'number' checks if the type of n is a number.
  • !isNaN(n) ensures the value is not NaN (Not a Number).
  • It ensures both type and value validity.

2. Check if a String is Numeric

Use regular expressions or isFinite() to validate numeric strings.

JavaScript
const isNumStr = s =>  	!isNaN(s) && isFinite(s);  console.log(isNumStr('123')); console.log(isNumStr('123abc'));  console.log(isNumStr('1.23'));  

Output
true false true 

In this example

  • !isNaN(s) ensures the string can be parsed as a number.
  • isFinite(s) ensures the value is not infinite.
  • This works for integers and decimals.

3. Validate Integer

Check if a number is an integer using Number.isInteger().

JavaScript
const isInt = n => Number.isInteger(n);  console.log(isInt(42));  console.log(isInt(3.14)); console.log(isInt('42')); 

Output
true false false 

In this example

  • Number.isInteger(n) directly validates if n is an integer.
  • It doesn't convert strings or other types to numbers.
  • The function is simple and precise.

4. Validate Floating-Point Numbers

Use a combination of checks to ensure a number is a float.

JavaScript
const isFloat = n => 	typeof n === 'number' && !Number.isInteger(n);  console.log(isFloat(3.14)); console.log(isFloat(42)); console.log(isFloat('3.14')); 

Output
true false false 

In this example

  • typeof n === 'number' ensures the value is a number.
  • !Number.isInteger(n) confirms it's not an integer.
  • This validates numbers with decimal points.

5. Range Validation

Ensure a number falls within a specific range.

JavaScript
const inRange = (n, min, max) => 	n >= min && n <= max;  console.log(inRange(10, 5, 15)); console.log(inRange(20, 5, 15)); console.log(inRange(5, 5, 15)); 

Output
true false true 

In this example

  • The function checks if n is greater than or equal to min and less than or equal to max.
  • It’s a quick way to validate ranges.

6. Regex for Strict Validation

Use regular expressions for specific number formats.

JavaScript
const isStrict = s => /^-?\d+(\.\d+)?$/.test(s);  console.log(isStrict('123')); console.log(isStrict('-123.45')); console.log(isStrict('abc123'));  

Output
true true false 

In this example

  • The regex ^-?\d+(\.\d+)?$ checks for optional negative signs, digits, and decimals.
  • This ensures strict numeric validation for strings.

7. Validate Positive Numbers

Check if a number is positive.

JavaScript
const isPos = n =>      typeof n === 'number' && n > 0;  console.log(isPos(42)); console.log(isPos(-42)); console.log(isPos(0)); 

Output
true false false 

In this example

  • typeof n === 'number' ensures the input is a number.
  • n > 0 validates that the number is positive.
  • It excludes zero and negative numbers.

8. Validate Negative Numbers

Check if a number is negative.

JavaScript
const isNeg = n =>      typeof n === 'number' && n < 0;  console.log(isNeg(-42)); console.log(isNeg(42)); console.log(isNeg(0)); 

Output
true false false 

In this example

  • typeof n === 'number' ensures the input is a number.
  • n < 0 checks if the number is negative.
  • It excludes zero and positive numbers.

9. Validate Even Numbers

Determine if a number is even.

JavaScript
const isEven = n =>      typeof n === 'number' && n % 2 === 0;  console.log(isEven(4)); console.log(isEven(7)); console.log(isEven(-2)); 

Output
true false true 

In this example

  • typeof n === 'number' ensures the input is a number.
  • n % 2 === 0 checks if the remainder is zero when divided by 2.
  • It works for both positive and negative numbers.

10. Validate Odd Numbers

Check if a number is odd.

JavaScript
const isOdd = n =>      typeof n === 'number' && n % 2 !== 0;  console.log(isOdd(7)); console.log(isOdd(4)); console.log(isOdd(-3)); 

Output
true false true 

In this example

  • typeof n === 'number' ensures the input is a number.
  • n % 2 !== 0 checks if the remainder is not zero when divided by 2.
  • It works for both positive and negative numbers.

Next Article
Number Validation in JavaScript

A

abhishekg25
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • HTML
  • JavaScript-Numbers
  • JavaScript-Questions

Similar Reads

    JavaScript Number valueOf( ) Method
    JavaScript Number valueOf( ) method in JavaScript is used to return the primitive value of a number. This method is usually called internally by JavaScript and not explicitly in web code Syntax: number.valueOf() Parameters: This method does not accept any parameter. Return Value: The valueof() metho
    1 min read
    JavaScript Numbers
    JavaScript numbers are primitive data types, and unlike other programming languages, you don't need to declare different numeric types like int, float, etc. JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754. This format stores numbers in 64 bits:0-51 bits store t
    6 min read
    How numbers are stored in JavaScript ?
    In this article, we will try to understand how numbers are stored in JavaScript. Like any other programming language, all the data is stored inside the computer in the form of binary numbers 0 and 1. Since computers can only understand and process data in the form of 0's and 1's. In JavaScript, ther
    6 min read
    Convert a String to Number in JavaScript
    To convert a string to number in JavaScript, various methods such as the Number() function, parseInt(), or parseFloat() can be used. These functions allow to convert string representations of numbers into actual numerical values, enabling arithmetic operations and comparisons.Below are the approache
    4 min read
    JavaScript Numbers Coding Practice Problems
    Numbers are fundamental in JavaScript, used for calculations, data analysis, and algorithm implementation. This curated list of JavaScript number practice problems covers essential concepts to master JavaScript Numbers. Whether you're a beginner or looking to sharpen your numerical computation skill
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences