Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
How to check if a Variable Is Not Null in JavaScript ?
Next article icon

How to Check if a Value is a Number in JavaScript ?

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number.

Methods to Check if a Value is a Number

There are various ways to check if a value is a number in JavaScript. Below are some common methods:

1. Using the isNaN() method

The isNaN() method is used to check if a value is not a Number. It returns true if the value is NaN (Not-a-Number) and false if the value can be converted into a valid number.

Syntax

isNaN(testingValue);

Now let's understand this with the help of an example:

JavaScript
function isNumber(value){     try{         if(isNaN(value)){             throw new Error("Passed value is not a number");         }         console.log("Passed value is a number");     }     catch(error){         console.log("An error occurred: ", error.message);     } }  isNumber(55); isNumber("58"); isNumber("GeeksforGeeks"); 

Output
Passed value is a number Passed value is a number An error occurred:  Passed value is not a number 

NOTE: The isNaN() method converts the value to a number before checking, which may lead to unexpected results in some cases.

2. Using the typeof operator

The typeof operator can be used to perform the type check of the passed value and compare it with the number type to check whether it is a number or not.

Syntax:

typeof testingValue;

Now let's understand this with the help of example:

JavaScript
function isNumber(value){     try{         if(!(typeof value === 'number')){             throw new Error("Passed value is not a number");         }         console.log("Passed value is a number");     }     catch(error){         console.log("An error occurred: ", error.message);     } }  isNumber(123); isNumber("123"); isNumber("GFG"); 

Output
Passed value is a number An error occurred:  Passed value is not a number An error occurred:  Passed value is not a number 

3. Using the Number.isInteger() method

The Number.isInteger() method can also be used to check if a value is number or not. It is mainly used for checking the integer values.

Syntax:

Number.isInteger(testingValue);

Now let's understand this with the help of example:

JavaScript
function isNumber(value){     try{         if(!(Number.isInteger(value))){             throw new Error("Passed value is not a number");         }         console.log("Passed value is a number");     }     catch(error){         console.log("An error occurred: ", error.message);     } }  isNumber(69); isNumber("231"); isNumber("Geek"); 

Output
Passed value is a number An error occurred:  Passed value is not a number An error occurred:  Passed value is not a number 

4. Using the Number.isFinite() method

The Number.isFinite() method is used to check if the passed value is finite or not. If it unables to parse the value it returns false and if it parses the value then it check if it is finit or infinite.

Syntax:

Number.isFinite(testingValue);

Now let's understand this with the help of example:

JavaScript
function isNumber(value){     try{         if(!(Number.isFinite(value))){             throw new Error("Passed value is not a number");         }         console.log("Passed value is a number");     }     catch(error){         console.log("An error occurred: ", error.message);     } }  isNumber(108); isNumber("11"); isNumber("Hello"); 

Output
Passed value is a number An error occurred:  Passed value is not a number An error occurred:  Passed value is not a number 

Why Checking if a Value is a Number is Important?

In JavaScript, not all values are of the "Number" type, and you might encounter:

  • Strings that represent numbers (e.g., "123").
  • NaN (Not-a-Number), which is a special value in JavaScript.
  • Other data types like objects, arrays, or undefined that may need validation before performing number-specific operations.

Conclusion

In JavaScript, checking if a value is a number is important for tasks like user input validation and calculations. Methods such as isNaN(), typeof, Number.isInteger(), parseInt(), and the unary plus (+) operator help easily determine if a value is a valid number, ensuring smooth data handling and avoiding errors.


Next Article
How to check if a Variable Is Not Null in JavaScript ?

A

abhish8rzd
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to check if a value is object-like in JavaScript ?
    In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
    4 min read
  • How to check if a Variable Is Not Null in JavaScript ?
    In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
    4 min read
  • How to Check if a Variable is an Array in JavaScript?
    To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
    2 min read
  • How to check if the value is primitive or not in JavaScript ?
    To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
    3 min read
  • How to check for null values in JavaScript ?
    The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau
    4 min read
  • How to Check if Object is JSON in JavaScript ?
    JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
    3 min read
  • JavaScript Program to Check if a Number is Float or Integer
    In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
    2 min read
  • How to Validate Decimal Numbers in JavaScript ?
    Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N
    2 min read
  • How to check whether a number is NaN or finite in JavaScript ?
    When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN
    3 min read
  • How to check if a number evaluates to Infinity using JavaScript ?
    The task is to check whether the number evaluates to infinity or not with the help of JavaScript. Here are a few techniques discussed. Approach 1: Checking if the number is equal to the Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY . Example: This example uses the approach discussed above. C/
    2 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