0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptの型変換の基礎: 暗黙的型変換と明示的型変換

Last updated at Posted at 2025-09-06

JavaScriptの型変換

JavaScriptは動的タイピング言語であり、変数のデータ型は状況に応じて変更される可能性があります。この型変換は、**暗黙的型変換(Implicit Coercion)明示的型変換(Explicit Conversion)**の2種類に分けられます。

1. 暗黙的型変換 (Implicit Coercion)

1.1 文字列への変換

  • 数値やブーリアン値が文字列と一緒に使用されるとき、文字列への変換が行われます。
console.log(1 + "2"); // "12"
console.log(true + "test"); // "truetest"

1.2 数値への変換

  • 文字列やブーリアン値が数値に変換されます。
  • 算術演算子(+-*/%)を使用するときに発生します。
console.log("10" - 5); // 5
console.log(true - 1); // 0
console.log("abc" * 2); // NaN

1.3 ブーリアンへの変換

  • 条件文などで真/偽を評価するために値がブーリアンに変換されます。
  • 以下の値はFalsy値です:
    • 00n(BigInt) 、 ""(空文字列)、nullundefinedNaN
  • それ以外はTruthy値です。
if ("") {
  console.log("Truthy");
} else {
  console.log("Falsy"); // 出力
}

1.4 オブジェクトと基本データ型間の変換

  • オブジェクトが数値または文字列に変換される際に、toString()またはvalueOf()メソッドが呼び出されます。
let obj = { valueOf: () => 42 };
console.log(obj + 1); // 43

let arr = [1, 2];
console.log(arr + ""); // "1,2"

2. 明示的型変換 (Explicit Conversion)

2.1 数値への変換

方法:

  1. Number()関数を使用
  2. 単項+演算子を使用
console.log(Number("123")); // 123
console.log(+"123"); // 123
console.log(Number("abc")); // NaN

2.2 文字列への変換

方法:

  1. String()関数を使用
  2. 値に空文字列("")を加算
console.log(String(123)); // "123"
console.log(123 + ""); // "123"

2.3 ブーリアンへの変換

方法:

  1. Boolean()関数を使用
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("hello")); // true
console.log(Boolean("")); // false

3. 型変換が頻繁に発生する状況

3.1 比較演算 (== vs ===)

  • ==(等価演算子)は型変換を実行します。
  • ===(厳密等価演算子)は型変換を実行せず、データ型まで比較します。
console.log(1 == "1"); // true
console.log(1 === "1"); // false

3.2 条件文

  • 条件文では値は常にブーリアンに変換されます。
if (0) {
  console.log("Truthy");
} else {
  console.log("Falsy"); // 出力
}

3.3 算術演算

  • 数値計算のために他の型が数値に変換されます。
console.log("5" * 2); // 10
console.log("5" + 2); // "52"(文字列結合)

4. 型変換に関する注意点

4.1 NaNの特性

  • NaNは自身自身とも等しくありません。
console.log(NaN === NaN); // false
console.log(Number("abc")); // NaN

4.2 Falsy値とTruthy値

  • 暗黙的型変換を理解していないと、条件文で意図しない動作を引き起こす可能性があります。
let value = 0;
if (value) {
  console.log("Truthy");
} else {
  console.log("Falsy"); // 出力
}

4.3 等価演算子 (==) の予期せぬ動作

console.log(null == undefined); // true
console.log(null === undefined); // false

4.4 オブジェクトの型変換

  • オブジェクトが文字列または数値に変換される際に、toString()valueOf()が呼び出されます。
let obj = {};
console.log(obj + 2); // "[object Object]2"

5. まとめ

  1. 暗黙的型変換は自動的に発生し、予期せぬ動作を引き起こす可能性があります。
  2. 明示的型変換はコードの可読性と安定性を高めるのに役立ちます。
  3. 型変換は特に条件文演算子比較演算で頻繁に発生します。
  4. 安全なコード作成のため、暗黙的変換よりも明示的変換を使用することを推奨します。
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?