在 JavaScript 中,用 isNaN() 檢查非法數字


以下為程式碼 −

示例

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);
   }
});

要執行上面的程式,你需要使用以下命令 −

node fileName.js.

此處,我的檔名是 demo201.js。

輸出

將輸出如下 −

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

更新於: 14-Sep-2020

162 瀏覽

開啟你的 職業生涯

完成該課程即可獲得認證

開始
廣告
© . All rights reserved.