Check for illegal number with isNaN() in JavaScript


Following is the code −

Example

function multiplication(firstValue, secondValue, callback) {
   var res = firstValue * secondValue;
   var err = isNaN(res) ? 'Something is wrong in input parameter' :
   undefined;
   callback(res, err);
}
multiplication(10, 50, function (result, error) {
   console.log("The multiplication result="+result);
   if (error) {
      console.log(error);
   }
});
multiplication('Sam', 5, function (result, error) {
   console.log("The multiplication result="+result);
   if (error) {
      console.log(error);
   }
});

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo201.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo201.js
The multiplication result=500
The multiplication result=NaN
Something is wrong in input parameter

Updated on: 14-Sep-2020

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements