JavaScriptのNumber型で使えるNumber.isIntegerメソッドとは?使い方とサンプルコードを徹底解説!


メソッドの構文

Number.isInteger(value);

このメソッドは、単一の引数 value を取ります。value は、整数かどうかを判定したい値を表す数値です。

メソッドの戻り値

  • 判定対象の値が整数でない場合: false を返します。
  • 判定対象の値が整数の場合: true を返します。

メソッドの動作

Number.isInteger メソッドは、内部的に IEEE 754 浮動小数点数の仕様に基づいて判定を行います。具体的には、以下の条件を満たす場合に true を返し、そうでない場合は false を返します。

  1. valueNaN ではないこと。
  2. valueInfinity ではないこと。
  3. value のべき乗が整数であること。
  4. value の小数部が 0 であること。

メソッドの例

console.log(Number.isInteger(10)); // true を出力
console.log(Number.isInteger(3.14)); // false を出力
console.log(Number.isInteger(Infinity)); // false を出力
console.log(Number.isInteger(NaN)); // false を出力
console.log(Number.isInteger(2e50)); // true を出力
console.log(Number.isInteger(2e50 + 0.00001)); // false を出力

メリット

  • コードの可読性と保守性を向上させることができる。
  • 型判定の際にエラーが発生しにくい。
  • 整数かどうかを簡潔に判定できる。
  • 非常に大きな整数や小さな整数を扱う場合は、BigInt 型を使用することを検討する必要があります。
  • Number.isInteger メソッドは、精度誤差の影響を受ける可能性があります。
  • 多くの JavaScript ランタイム環境でサポートされています。
  • Number.isInteger メソッドは、ECMAScript 2015 (ES6) で導入されました。


例 1:基本的な使用方法

この例では、Number.isInteger メソッドを使用して、さまざまな値が整数かどうかを判定します。

console.log(Number.isInteger(10)); // true を出力
console.log(Number.isInteger(3.14)); // false を出力
console.log(Number.isInteger('10')); // false を出力
console.log(Number.isInteger(parseInt('10'))); // true を出力
console.log(Number.isInteger(true)); // false を出力
console.log(Number.isInteger(false)); // false を出力

例 2:小数点以下の桁数による判定

この例では、Number.isInteger メソッドを使用して、小数点以下の桁数が 0 であるかどうかを判定します。

function isDecimalZero(value) {
  return Number.isInteger(value) && value % 1 === 0;
}

console.log(isDecimalZero(10)); // true を出力
console.log(isDecimalZero(10.0)); // true を出力
console.log(isDecimalZero(10.001)); // false を出力

例 3:範囲内の整数を判定

この例では、Number.isInteger メソッドと条件分岐を使用して、特定の範囲内の整数を判定します。

function isInRange(value, min, max) {
  return Number.isInteger(value) && value >= min && value <= max;
}

console.log(isInRange(10, 5, 15)); // true を出力
console.log(isInRange(20, 5, 15)); // false を出力
console.log(isInRange('10', 5, 15)); // false を出力

この例では、Number.isInteger メソッドと BigInt 型を使用して、非常に大きな整数を判定します。

const bigIntValue = 9007199254740991n;

console.log(Number.isInteger(bigIntValue)); // false を出力
console.log(BigInt.isInteger(bigIntValue)); // true を出力

これらの例は、Number.isInteger メソッドの使用方法を理解するための出発点として役立ちます。実際の開発においては、状況に応じて適切な使用方法を選択してください。

  • 数学的な計算において、整数の値を扱う。
  • 整数のみを含む配列を生成する。
  • 数値入力を検証して、エラーメッセージを適切に表示する。
  • 特定の条件に基づいて、整数のリストをフィルタリングする。


parseInt() 関数

parseInt() 関数は、文字列を整数に変換する関数です。引数として文字列と基数(オプション)を受け取り、文字列の先頭から基数に応じた整数部分を返します。

function isInteger(value) {
  return parseInt(value, 10) === value;
}

console.log(isInteger(10)); // true を出力
console.log(isInteger(3.14)); // false を出力
console.log(isInteger('10')); // true を出力
console.log(isInteger('10.0')); // false を出力

利点

  • 比較的古いブラウザでも動作する
  • シンプルでわかりやすいコード

欠点

  • 文字列以外の値を判定できない
  • 浮動小数点数の誤差の影響を受ける可能性がある

isFinite() 関数と剰余演算子

isFinite() 関数は、数値が有限かどうかを判定する関数です。Number.isInteger メソッドと同様に、NaNInfinity を除いた有限な数値に対して true を返します。

剰余演算子 (%) は、二つのオペランドの剰余を計算します。整数同士の剰余は常に 0 になりますので、この性質を利用して整数を判定することができます。

function isInteger(value) {
  return isFinite(value) && value % 1 === 0;
}

console.log(isInteger(10)); // true を出力
console.log(isInteger(3.14)); // false を出力
console.log(isInteger('10')); // false を出力
console.log(isInteger(Infinity)); // false を出力

利点

  • 浮動小数点数の誤差の影響を受けにくい
  • Number.isInteger メソッドよりも古いブラウザで動作する

欠点

  • 判定ロジックがやや冗長

正規表現

正規表現は、文字列のパターンを照合する強力なツールです。整数のパターンを表す正規表現を使用して、文字列が整数かどうかを判定することができます。

function isInteger(value) {
  return /^-?\d+$/.test(value);
}

console.log(isInteger(10)); // true を出力
console.log(isInteger(3.14)); // false を出力
console.log(isInteger('10')); // true を出力
console.log(isInteger('10.0')); // false を出力
console.log(isInteger('abc')); // false を出力

利点

  • 複雑な判定条件にも対応できる
  • 柔軟性が高く、様々な形式の整数を判定できる

欠点

  • 正規表現の構文が複雑でわかりにくい
  • 処理速度が比較的遅い

上記の代替方法に加えて、独自の判定ロジックを持つカスタム関数を作成することもできます。これは、特定の状況や要件に合わせた判定を行う必要がある場合に有効です。

function isInteger(value) {
  // 独自の判定ロジックを実装
  if (typeof value !== 'number') return false;
  if (isNaN(value) || !isFinite(value)) return false;
  if (Math.abs(value) < 1) return false;
  return value % 1 === 0;
}

console.log(isInteger(10)); // true を出力
console.log(isInteger(3.14)); // false を出力
console.log(isInteger('10')); // false を出力
console.log(isInteger(Infinity)); // false を出力
console.log(isInteger(new BigInt(10))); // true を出力

利点

  • 状況に特化した判定が可能
  • 完全な制御性と柔軟性
  • コードの保守性が低下する可能性がある
  • テストとデバッグが複雑になる可能性がある