Using Number isNaN() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the Number technique referred to as isNaN() with syntax and examples.

Description

In JavaScript, isNaN() is a Number technique that is used to return a Boolean value indicating whether a price is of kind Number with a fee of NaN. Because isNaN() is a technique of the Number object, it have to be invoked thru the object called Number.

Syntax

In JavaScript, the syntax for the isNaN() method is:

Number.isNaN(value);

Parameters or Arguments

value The fee to take a look at whether it is of type Number with a fee of NaN.

Returns

The isNaN() method returns proper if the value is NaN and of kind Number. Otherwise, it returns false.

Example

Let’s take a seem to be at an example of how to use the isNaN() technique in JavaScript.

For example:

console.log(Number.isNaN(NaN));
console.log(Number.isNaN(6.7));
console.log(Number.isNaN('6.7'));

In this example, we have invoked the isNaN() technique using the Number class.

We have written the output of the isNaN() approach to the net browser console log, for demonstration purposes, to exhibit what the isNaN() method returns.

The following will be output to the internet browser console log:

true
false
false

In this example, the first output to the console log back real considering the value is NaN.

The 2d output to the console log lower back false since 6.7 is now not equal to NaN.

The 1/3 output to the console log lower back false seeing that the string price ‘6.7’ is now not equal to NaN.

Using isNaN() technique to decide if a price is a quantity

You can use the isNaN() technique to determine if a cost is a wide variety or not.

For example:

var totn_number = Number('ABC123');

if (Number.isNaN(totn_number)) {
    console.log('Value is not a number');

} else {
    console.log('Value is a number');
}

The following will be output to the internet browser console log:

Value is not a number

In this example, totn_number will have a value of NaN considering that ‘ABC123’ is not a number. Therefore, ‘Value is no longer a number’ is output to the console log.

Using isNaN() method with Other Functions

You can also use the isNaN() technique to evaluate the output of other features in JavaScript.

The following Math functions can output NaN as a result:

abs() acos() acosh() asin() atanh() log() log10() log1p() log2() max() min() sqrt()

Let’s appear at how you may use the isNaN() method to evaluate the output from the abs() technique when a non-numeric fee is passed in as a parameter.

For example:

var totn_number = 'ABC123';

if (Number.isNaN(Math.abs(totn_number))) {
    console.log('abs parameter was not a number');

} else {
    console.log('abs parameter was a number');
}

The following will be output to the web browser console log:

abs parameter was not a number

In this example, the parameter handed into the abs() approach was the price ‘ABC123’ which is not a wide variety (NaN). This causes the isNaN() method to return actual and output ‘abs parameter used to be no longer a number’ to the console log.