JavaScript

isNumber JavaScript

There can be a scenario to find the entries that have been inserted into the wrong fields. For instance, in the case of applying a check upon the “date” field while filling out a particular form. Moreover, it is also beneficial in the case of configuring the encoded or decoded items. In such scenarios, the “isNumber()” method is very helpful in distinguishing between the numbers and strings.

This write-up will explain the usage of the isNumber() method in JavaScript with the help of different examples.

What is “isNumber()” in JavaScript?

The “_.isNumber()” is a JavaScript predefined method used to check whether the specified argument is a number or not based on the boolean values “true” or “false”. This function returns the boolean value “true” in the case of an integer or a floating-point value; otherwise returns a “false”.

Syntax

_.isNumber(object)

In the given syntax, the “object” holds the element that needs to be checked for a number.

Example 1: Using _.isNumber() Method to Check Numeric Type

In this example, the _.isNumber() function can be applied to return a corresponding boolean value against each of the provided values in its argument.

To do so, firstly, apply the function upon the string value “Number”:

console.log(_.isNumber('Number'));

Next, print the corresponding output against the following integer value:

console.log(_.isNumber(10));

Lastly, check the specified function on the float value and return the corresponding boolean value:

console.log(_.isNumber(10.2));

In the above output, it can be observed that the boolean value “false” is returned against the provided string value and the boolean value “true” is returned against both the integer and float values.

Example 2: Applying _.isNumber() to Check the Array Elements

In the following example, we will apply the isNumber() method on the declared array to check if the accessed array element is a number or a string.

First, we will declare an array that contains the integer and string values respectively:

array= [1, 2, "number"]

Next, access the array elements with the specified indexes. Both of the accessed elements will output different boolean values based on their data types:

console.log(_.isNumber(array[1]))
console.log(_.isNumber(array[2]))

The output displayed “true” against the first index value that is “2” and “false” for the 2nd index that is a string:

We have implemented various examples for implementing the “isNumber()” method.

Conclusion

isNumber()” is a built-in JavaScript method used to verify the strings, whether it is a number or not. This method takes an argument and checks whether it is a number or not and returns the boolean value “true” or false” accordingly. It is very helpful to distinguish between numbers and strings. This blog illustrated the use of the isNumber() method in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.