How to check if a value is a number in JavaScript

#javascript

Last updated on Mar 28, 2022 by Suraj Sharma



In this tutorial, you’ll learn two ways to check if a given value is a number in JavaScript


1. Using isNaN()


The isNaN() function determines whether the passed argument is NaN(not a number) or not.

It returns false if the passed argument is a number else returns true.


Example

isNaN(45) // false
isNaN('Hello') // true
isNaN(NaN) // true
isNaN(45.02) // false
isNaN('45') // false


2. Using typeof operation


The typeof operator returns a string representing the type of a value/operand.


Example

typeof 45 === 'number' // true
typeof 'hello' === 'number' // false
typeof 56.78 === 'number' // true


Related Solutions


Rate this post


Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.