aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMasahiro FUJIMOTO <mfujimot@gmail.com>2022-02-18 10:28:13 +0900
committerMasahiro FUJIMOTO <mfujimot@gmail.com>2022-02-26 16:04:26 +0900
commit4d50bcf05516c7c368266acb25a39dd6802d0464 (patch)
treea2e4c4fa851e09e2b55612d69e539f3f64c4d448
parentf6ceeec73546dae8d4b7847cc81742dc8bae3abb (diff)
downloadtranslated-content-4d50bcf05516c7c368266acb25a39dd6802d0464.tar.gz
translated-content-4d50bcf05516c7c368266acb25a39dd6802d0464.tar.bz2
translated-content-4d50bcf05516c7c368266acb25a39dd6802d0464.zip
2022/02/04 時点の英語版に同期
-rw-r--r--files/ja/web/javascript/reference/operators/nullish_coalescing_operator/index.md178
1 files changed, 86 insertions, 92 deletions
diff --git a/files/ja/web/javascript/reference/operators/nullish_coalescing_operator/index.md b/files/ja/web/javascript/reference/operators/nullish_coalescing_operator/index.md
index 20eaf23925..3d74a548ce 100644
--- a/files/ja/web/javascript/reference/operators/nullish_coalescing_operator/index.md
+++ b/files/ja/web/javascript/reference/operators/nullish_coalescing_operator/index.md
@@ -1,147 +1,141 @@
---
-title: Null 合体 (??)
+title: Null 合体演算子 (??)
slug: Web/JavaScript/Reference/Operators/Nullish_coalescing_operator
tags:
- JavaScript
- - Language feature
- - Operator
- - Reference
+ - 言語機能
+ - 演算子
+ - リファレンス
- nullish coalescing
+browser-compat: javascript.operators.nullish_coalescing
translation_of: Web/JavaScript/Reference/Operators/Nullish_coalescing_operator
---
-<p>{{JSSidebar("Operators")}}</p>
+{{JSSidebar("Operators")}}
-<p><strong>Null 合体演算子 (<code>??</code>)</strong> は論理演算子の一種です。この演算子は左辺が {{jsxref("null")}} または {{jsxref("undefined")}} の場合に右の値を返し、それ以外の場合に左の値を返します。</p>
+**Null 合体演算子 (`??`)** は論理演算子の一種です。この演算子は左辺が {{jsxref("null")}} または {{jsxref("undefined")}} の場合に右の値を返し、それ以外の場合に左の値を返します。
-<p><a href="/ja/docs/Web/JavaScript/Reference/Operators/Logical_Operators">OR 演算子 (<code>||</code>)</a> と違い、<code>null</code> と <code>undefined</code> 以外の <em><a href="/ja/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description">falsy</a></em> な値のときには左の値を返します。つまり、左辺が <code>''</code> や <code>0</code> の場合は左の値を評価して返します。その他の例については以下を参照してください。</p>
+これは[論理 OR 演算子 (`||`)](/ja/docs/Web/JavaScript/Reference/Operators/Logical_OR) の特殊形と見なすことができます。そちらは左辺の値が `null` や `undefined` だけでなく、何らかの{{Glossary("falsy", "偽値")}}であった場合に右辺値を返すものです。つまり、 `||` を使って別の変数 `foo` に何らかの既定値を与える場合、一部の偽値(例えば `''` や `0`)を使用可能とみなすと、予想外の動作に遭遇することがあります。詳しい例は以下を参照してください。
-<div>{{EmbedInteractiveExample("pages/js/expressions-nullishcoalescingoperator.html")}}</div>
+Null 合体演算子は[演算子の優先順位](/ja/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)が下から 5 番目で、 `||` のすぐ下、[条件(三項)演算子](/ja/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)のすぐ上とします。
-<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.<br>
- See <a href="https://github.com/mdn/interactive-examples/pull/1482#issuecomment-553841750">PR #1482</a> regarding the addition of this example.</p>
+{{EmbedInteractiveExample("pages/js/expressions-nullishcoalescingoperator.html")}}
-<h2 id="Syntax" name="Syntax">構文</h2>
+## 構文
-<pre class="syntaxbox notranslate"><var>leftExpr</var> ?? <var>rightExpr</var>
-</pre>
+```js
+leftExpr ?? rightExpr
+```
-<h2 id="Description" name="Description">説明</h2>
+## 例
-<p>Null 合体演算子は左辺が {{jsxref("null")}} または {{jsxref("undefined")}} の場合に右辺の値を返します。</p>
+### Null 合体演算子の使用
-<h3 id="Assigning_a_default_value_to_a_variable" name="Assigning_a_default_value_to_a_variable">変数にデフォルト値を代入する</h3>
+次の例では、既定値を設定していますが、`null` や `undefined` 以外の値は保持されます。
-<p>以前は、変数にデフォルト値を代入したい場合、一般的なパターンは OR 演算子 (<code><a href="/ja/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR_2">||</a></code>) を使用することでした:</p>
+```js
+const nullValue = null;
+const emptyText = ""; // falsy
+const someNumber = 42;
+
+const valA = nullValue ?? "default for A";
+const valB = emptyText ?? "default for B";
+const valC = someNumber ?? 0;
+
+console.log(valA); // "default for A"
+console.log(valB); // "" (空文字列は null でも undefined でもないため)
+console.log(valC); // 42
+```
+
+### 変数に既定値を代入する
-<pre class="brush: js notranslate">let foo;
+以前は、変数に既定値を代入したい場合の一般的なパターンは、論理 OR 演算子([`||`](/ja/docs/Web/JavaScript/Reference/Operators/Logical_OR))を使用することでした。
-// foo is never assigned any value so it is still undefined
-let someDummyText = foo || 'Hello!';</pre>
+```js
+let foo;
-<p>しかし、<code>||</code> が論理演算子であるため、左辺の値は評価によって強制的にブール値になり、falsy な値 (<code>0</code>, <code>''</code>, <code>NaN</code>, <code>null</code>, <code>undefined</code>) が返されることはありません。この動作は、<code>0</code> や <code>''</code>, <code>NaN</code> を有効な値と考えている場合、予期せぬ結果を引き起こす可能性があります。</p>
+// foo には何も値が代入されていないので、 undefined のままです
+let someDummyText = foo || 'Hello!';
+```
-<pre class="brush: js notranslate">let count = 0;
+しかし、`||` が論理演算子であるため、左辺の値は評価によって強制的に論理値になり、偽値(`0`, `''`, `NaN`, `null`, `undefined`)が返されることはありません。この動作は、 `0` や `''`, `NaN` を有効な値と考えている場合、予期せぬ結果を引き起こす可能性があります。
+
+```js
+let count = 0;
let text = "";
let qty = count || 42;
let message = text || "hi!";
-console.log(qty); // 42 and not 0
-console.log(message); // "hi!" and not ""
-</pre>
+console.log(qty); // 42 であり 0 ではない
+console.log(message); // "hi!" であり "" ではない
+```
-<p>Null 合体演算子は、左辺の値が <code>null</code> もしくは <code>undefined</code> のどちらか (その他の falsy な値は含みません) に評価された場合にのみ右辺の値を返すことで、この潜在的な危険を回避します:</p>
+Null 合体演算子は、左辺の値が `null` もしくは `undefined` のどちらか(その他の falsy な値は含みません)に評価された場合にのみ右辺の値を返すことで、この潜在的な危険を回避します。
-<pre class="brush: js notranslate">let myText = ''; // An empty string (which is also a falsy value)
+```js
+let myText = ''; // 空文字列(偽値)
let notFalsyText = myText || 'Hello world';
console.log(notFalsyText); // Hello world
let preservingFalsy = myText ?? 'Hi neighborhood';
-console.log(preservingFalsy); // '' (as myText is neither undefined nor null)
-</pre>
+console.log(preservingFalsy); // '' (myText は undefined でも null でもない)
+```
-<h3 id="Short-circuiting" name="Short-circuiting">短絡評価</h3>
+### 短絡評価
-<p>OR 演算子や AND 演算子と同様に、左辺が <code>null</code> でも <code>undefined</code> でもないことが証明された場合、右辺の式は評価されません。</p>
+OR 演算子や AND 演算子と同様に、左辺が `null` でも `undefined` でもないことが判明した場合、右辺の式は評価されません。
-<pre class="brush: js notranslate">function A() { console.log('A was called'); return undefined;}
+```js
+function A() { console.log('A was called'); return undefined;}
function B() { console.log('B was called'); return false;}
function C() { console.log('C was called'); return "foo";}
console.log( A() ?? C() );
-// logs "A was called" then "C was called" and then "foo"
-// as A() returned undefined so both expressions are evaluated
+// "A was called"、 "C was called" のあと "foo" と出力
+// A() は undefined を返すため、両方の式が評価されるため
console.log( B() ?? C() );
-// logs "B was called" then "false"
-// as B() returned false (and not null or undefined), the right
-// hand side expression was not evaluated
-</pre>
+// "B was called" のあと "false" と出力
+// B() は false を返すため(そして null も undefined も返さない)、
+// 右辺の式は評価されない
+```
-<h3 id="No_chaining_with_AND_or_OR_operators" name="No_chaining_with_AND_or_OR_operators">AND 演算子、OR 演算子とつなげて使わない</h3>
+### AND 演算子、OR 演算子とつなげて使わない
-<p>AND 演算子 (<code>&amp;&amp;</code>) と OR 演算子 (<code>||</code>) を直接 <code>??</code> とつなげて使うことはできません。このような場合 <code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code> が発生します。</p>
+AND 演算子 (`&&`) と OR 演算子 (`||`) を直接 `??` と組み合わせて使うことはできません。このような場合 [`SyntaxError`](/ja/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) が発生します。
-<pre class="brush: js example-bad notranslate">null || undefined ?? "foo"; // raises a SyntaxError
-true || undefined ?? "foo"; // raises a SyntaxError</pre>
+```js example-bad
+null || undefined ?? "foo"; // SyntaxError が発生
+true || undefined ?? "foo"; // SyntaxError が発生
+```
-<p>ただし、カッコを付けて明示的に優先順位を示すのは正しいやり方です。</p>
+ただし、カッコを付けて明示的に優先順位を示すのは正しいやり方です。
-<pre class="brush: js example-good notranslate">(null || undefined) ?? "foo"; // returns "foo"
-</pre>
+```js example-good
+(null || undefined) ?? "foo"; // "foo" を返す
+```
-<h3 id="Relationship_with_the_optional_chaining_operator_." name="Relationship_with_the_optional_chaining_operator_.">オプショナルチェイニング演算子 (<code>?.</code>) との関係</h3>
+### オプション連鎖演算子 (`?.`) との関係
-<p>Null 合体演算子は、<code>null</code> と <code>undefined</code> を特定の値として扱いますが、<a href="/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining">オプショナルチェイニング演算子 (<code>?.</code>)</a> も同様の扱いをします。この演算子は、<code>null</code> または <code>undefined</code> である可能性のあるオブジェクトのプロパティにアクセスするのに便利です。</p>
+Null 合体演算子は、 `undefined` と `null` を特定の値として扱いますが、[オプション連鎖演算子 (`?.`)](/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining) も同様の扱いをします。この演算子は、`null` または `undefined` である可能性のあるオブジェクトのプロパティにアクセスするのに便利です。
-<pre class="brush: js notranslate">let foo = { someFooProp: "hi" };
+```js
+let foo = { someFooProp: "hi" };
-console.log(foo.someFooProp?.toUpperCase()); // "HI"
-console.log(foo.someBarProp?.toUpperCase()); // undefined
-</pre>
+console.log(foo.someFooProp?.toUpperCase() ?? "not available"); // "HI"
+console.log(foo.someBarProp?.toUpperCase() ?? "not available"); // "not available"
+```
-<h2 id="Examples" name="Examples">例</h2>
+## 仕様書
-<p>次の例では、デフォルト値を設定していますが、<code>null</code> や <code>undefined</code> 以外の値は保持されます。</p>
+{{Specifications}}
-<pre class="brush: js notranslate">const nullValue = null;
-const emptyText = ""; // falsy
-const someNumber = 42;
+## ブラウザーの互換性
-const valA = nullValue ?? "default for A";
-const valB = emptyText ?? "default for B";
-const valC = someNumber ?? 0;
+{{Compat}}
-console.log(valA); // "default for A"
-console.log(valB); // "" (as the empty string is not null or undefined)
-console.log(valC); // 42
-</pre>
-
-<h2 id="Specifications" name="Specifications">仕様</h2>
-
-<table class="standard-table">
- <thead>
- <tr>
- <th scope="col">仕様書</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>
- <p>{{SpecName('ESDraft', '#prod-Nulli', 'nullish coalescing expression')}}</p>
- </td>
- </tr>
- </tbody>
-</table>
-
-<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザー実装状況</h2>
-
-<p>{{Compat("javascript.operators.nullish_coalescing")}}</p>
-
-<h2 id="See_also" name="See_also">関連情報</h2>
-
-<ul>
- <li><a href="/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining">オプショナルチェイニング演算子</a></li>
- <li><a href="/ja/docs/Web/JavaScript/Reference/Operators/Logical_Operators">論理 OR (<code>||</code>) 演算子</a></li>
- <li><a href="/ja/docs/Web/JavaScript/Reference/Functions/Default_parameters">デフォルト引数</a></li>
-</ul>
+## 関連情報
+
+- [オプション連鎖演算子](/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
+- [論理 OR (`||`) 演算子](/ja/docs/Web/JavaScript/Reference/Operators/Logical_OR)
+- [デフォルト引数](/ja/docs/Web/JavaScript/Reference/Functions/Default_parameters)