Check if a variable is a number in JavaScript

In this short article, we’ll show you how to check if a variable is a number in JavaScript. Here, you will find the easiest way to check if the value is a number or not. Let’s start with examples.

Ways to check if a variable is a number in JavaScript

  1. isNaN() function
  2. typeof operator

1. isNaN() function

The first one is isNaN(), a global variable that stands for is Not a Number. This function returns false if value is a number.

Syntax:

To check if a variable is a number, we will write condition like below:

2. typeof operator

The typeof operator uses for the check the type of the variable and returns the string. JavaScript has 9 types as below:

  • undefined
  • null
  • boolean
  • number
  • string
  • bigint
  • object
  • symbol
  • function (a special type of object)

Let’s take an example.

That’s it for today.
Thank you for reading. Happy Coding..!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *