JavaScript isNaN()関数で数値チェックを行う

環境
Windows 10 home 64bit
Google Chrome 107.0.5304.122(Official Build) (64 ビット)

構文
isNaN(value)
引数
value テストされる値
戻り値
渡された値が NaN である場合は true を返し, そうでなければ false を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function test(x) {
if (isNaN(x)) {
return '数字ではない';
}
return x * 100;
}
console.log(test('100F'));
console.log(test('0.0534E+2'));
function test(x) { if (isNaN(x)) { return '数字ではない'; } return x * 100; } console.log(test('100F')); console.log(test('0.0534E+2'));
function test(x) {
  if (isNaN(x)) {
    return '数字ではない';
  }
  return x * 100;
}

console.log(test('100F'));
console.log(test('0.0534E+2'));

実行結果
> “数字ではない"
> 534

JavaScript

Posted by arkgame