How to check if a value is a number in JavaScript

#javascript

Last updated on Oct 4, 2021 by Suraj Sharma



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


1. UsingisNaN()


The window.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


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.