{"id":6532,"date":"2026-09-18T14:44:21","date_gmt":"2026-09-18T05:44:21","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/?post_type=route&#038;p=6532"},"modified":"2026-11-02T14:45:29","modified_gmt":"2026-11-02T05:45:29","slug":"javascript-nullish-coalescing-vs-or-default-value","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/6532\/","title":{"rendered":"JavaScript\u306e??\u3068||\u306e\u9055\u3044\uff5c0\u30fb\u7a7a\u6587\u5b57\u3092\u6d88\u3055\u306a\u3044\u66f8\u304d\u65b9"},"content":{"rendered":"<p>JavaScript\u3067\u5024\u306e\u521d\u671f\u8a2d\u5b9a\u3092\u3059\u308b\u3068\u304d\u3001`||` \u306f `0` \u3084\u7a7a\u6587\u5b57 `&quot;&quot;` \u307e\u3067\u300c\u5024\u304c\u306a\u3044\u300d\u3068\u6271\u3044\u307e\u3059\u3002null\u30fbundefined\u3060\u3051\u3092\u7f6e\u304d\u63db\u3048\u305f\u3044\u306a\u3089 `??` \u304c\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u5b9f\u884c\u7d50\u679c\u3092\u898b\u3066\u9078\u3073\u65b9\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>??\u3068||\u306e\u9055\u3044\u306f\u300c\u7f6e\u304d\u63db\u3048\u308b\u6761\u4ef6\u300d<\/h2>\n<p>`a || b` \u306f\u5de6\u306e `a` \u304c falsy\uff08\u771f\u507d\u5224\u5b9a\u3067 false \u3068\u6271\u308f\u308c\u308b\u5024\uff09\u306e\u3068\u304d\u306b `b` \u3092\u8fd4\u3057\u307e\u3059\u3002`a ?? b` \u306f `a` \u304c `null` \u307e\u305f\u306f `undefined` \u306e\u3068\u304d\u3060\u3051 `b` \u3092\u8fd4\u3057\u307e\u3059\u3002\u3069\u3061\u3089\u3082\u3001\u5de6\u5074\u3067\u7d50\u679c\u304c\u6c7a\u307e\u308c\u3070\u53f3\u5074\u3092\u8a55\u4fa1\u3057\u306a\u3044\u77ed\u7d61\u8a55\u4fa1\u3067\u3059\u3002<\/p>\n<blockquote>\n<p><strong>\u7528\u8a9e\u89e3\u8aac\uff1afalsy\uff08\u30d5\u30a9\u30eb\u30b7\u30fc\uff09<\/strong><br \/>JavaScript\u3067\u6761\u4ef6\u5224\u5b9a\u6642\u306b false \u3068\u6271\u308f\u308c\u308b\u5024\u3002\u4f8b\u3048\u3070 0\u3001\u7a7a\u6587\u5b57\u3001false\u3001null\u3001undefined\u3001NaN \u306a\u3069\u3002\u30c7\u30fc\u30bf\u3068\u3057\u3066\u7121\u52b9\u3068\u540c\u3058\u610f\u5473\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<\/blockquote>\n<blockquote>\n<p><strong>\u7528\u8a9e\u89e3\u8aac\uff1anull \/ undefined<\/strong><br \/>null \u306f\u300c\u5024\u304c\u306a\u3044\u300d\u3053\u3068\u3092\u660e\u793a\u3059\u308b\u5024\u3001undefined \u306f\u4ee3\u5165\u3055\u308c\u3066\u3044\u306a\u3044\u5909\u6570\u3084\u5b58\u5728\u3057\u306a\u3044\u30d7\u30ed\u30d1\u30c6\u30a3\u306a\u3069\u3067\u73fe\u308c\u308b\u5024\u3067\u3059\u3002<\/p>\n<\/blockquote>\n<table>\n<thead>\n<tr>\n<th>\u5de6\u306e\u5024<\/th>\n<th>`\u5de6 || &quot;\u65e2\u5b9a&quot;`<\/th>\n<th>`\u5de6 ?? &quot;\u65e2\u5b9a&quot;`<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>`0`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<td>`0`<\/td>\n<\/tr>\n<tr>\n<td>`&quot;&quot;`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<td>`&quot;&quot;`<\/td>\n<\/tr>\n<tr>\n<td>`false`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<td>`false`<\/td>\n<\/tr>\n<tr>\n<td>`null`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<\/tr>\n<tr>\n<td>`undefined`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<\/tr>\n<tr>\n<td>`NaN`<\/td>\n<td>`&quot;\u65e2\u5b9a&quot;`<\/td>\n<td>`NaN`<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8868\u306e `&quot;&quot;` \u306f\u9577\u30550\u306e\u6587\u5b57\u5217\u3001`NaN` \u306f\u6570\u5024\u3068\u3057\u3066\u8a08\u7b97\u3067\u304d\u306a\u3044\u7d50\u679c\u306a\u3069\u3092\u8868\u3057\u307e\u3059\u3002\u3069\u3061\u3089\u3082 `??` \u306a\u3089\u305d\u306e\u307e\u307e\u6b8b\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u5728\u5eab\u6570\u306e `0` \u306f\u300c\u54c1\u5207\u308c\u300d\u3068\u3044\u3046\u610f\u5473\u306e\u3042\u308b\u5024\u3067\u3059\u3002<\/p>\n<hr>\n<h2>\u30d6\u30e9\u30a6\u30b6\u30fc\u30670\u30fb\u7a7a\u6587\u5b57\u30fbfalse\u3092\u6bd4\u8f03\u3057\u3066\u307f\u308b<\/h2>\n<p>Chrome\u306a\u3069\u306e\u958b\u767a\u8005\u30c4\u30fc\u30eb\u3067 Console \u3092\u958b\u304d\u3001\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u305d\u306e\u307e\u307e\u8cbc\u3063\u3066\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002Node.js\u304c\u4f7f\u3048\u308b\u5834\u5408\u306f `.js` \u30d5\u30a1\u30a4\u30eb\u3078\u4fdd\u5b58\u3057\u3066 `node \u30d5\u30a1\u30a4\u30eb\u540d.js` \u3067\u3082\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>const stock = 0;\r\nconst nickname = &quot;&quot;;\r\nconst checked = false;\r\n\r\nconsole.log(stock || 10);                  \/\/ 10\r\nconsole.log(stock ?? 10);                  \/\/ 0\r\nconsole.log(nickname || &quot;\u30b2\u30b9\u30c8&quot;);         \/\/ \u30b2\u30b9\u30c8\r\nconsole.log(JSON.stringify(nickname ?? &quot;\u30b2\u30b9\u30c8&quot;)); \/\/ \u7a7a\u6587\u5b57\u5217\u3092\u793a\u3059 &quot;&quot; \u3092\u8868\u793a\r\nconsole.log(checked || true);              \/\/ true\r\nconsole.log(checked ?? true);              \/\/ false\r\nconsole.log(null ?? &quot;\u672a\u8a2d\u5b9a&quot;);            \/\/ \u672a\u8a2d\u5b9a<\/code><\/pre>\n<p>\u3053\u3053\u3067\u898b\u305f\u3044\u306e\u306f `||` \u3067\u306f `0`\u3001\u7a7a\u6587\u5b57\u3001`false` \u304c\u53f3\u8fba\u306b\u7f6e\u304d\u63db\u308f\u308b\u4e00\u65b9\u3001`??` \u3067\u306f\u5143\u306e\u5024\u304c\u6b8b\u308b\u3053\u3068\u3067\u3059\u3002\u7a7a\u6587\u5b57\u306f\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u898b\u843d\u3068\u3057\u3084\u3059\u3044\u305f\u3081 `JSON.stringify` \u3092\u4f7f\u3063\u3066 `&quot;&quot;` \u3068\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3057\u3066\u7d42\u308f\u308a\u306b\u305b\u305a\u3001`stock` \u3092 `5`\u3001`null`\u3001`undefined` \u306b\u5909\u3048\u3066\u5404\u884c\u306e\u7d50\u679c\u3092\u4e88\u60f3\u3057\u3066\u304b\u3089\u5b9f\u884c\u3059\u308b\u3068\u3001\u6761\u4ef6\u306e\u9055\u3044\u3092\u7406\u89e3\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<hr>\n<h2>\u5165\u529b\u30d5\u30a9\u30fc\u30e0\u3067\u5024\u3092\u6d88\u3055\u306a\u3044\u305f\u3081\u306e\u9078\u3073\u65b9<\/h2>\n<p>\u4ef6\u6570\u30fb\u6570\u91cf\u30fbON\/OFF\u72b6\u614b\u306e\u3088\u3046\u306b `0` \u3084 `false` \u304c\u610f\u5473\u3092\u6301\u3064\u5834\u5408\u3001\u672a\u8a2d\u5b9a\u3060\u3051\u3092\u88dc\u3046\u7528\u9014\u306b\u306f `??` \u3092\u9078\u3073\u307e\u3059\u3002\u4e00\u65b9\u3001\u7a7a\u6587\u5b57\u3082\u300c\u672a\u5165\u529b\u300d\u3068\u3057\u3066\u6271\u3044\u305f\u3044\u306a\u3089\u3001`??` \u3060\u3051\u3067\u306f\u4e0d\u8db3\u3067\u3059\u3002<\/p>\n<pre><code>const submittedName = &quot;&quot;;\r\nconst safeName = submittedName.trim() === &quot;&quot;\r\n  ? &quot;\u30b2\u30b9\u30c8&quot;\r\n  : submittedName;\r\nconsole.log(safeName); \/\/ \u30b2\u30b9\u30c8<\/code><\/pre>\n<p>\u3053\u306e\u4f8b\u306f `submittedName` \u304c\u6587\u5b57\u5217\u3067\u3042\u308b\u524d\u63d0\u3067\u3059\u3002`null` \u306e\u53ef\u80fd\u6027\u3082\u3042\u308b\u306a\u3089\u5148\u306b `const submittedName = rawName ?? &quot;&quot;;` \u306e\u3088\u3046\u306b\u6271\u3044\u3092\u6c7a\u3081\u3066\u304b\u3089 `trim()` \u3092\u547c\u3073\u307e\u3059\u3002\u76ee\u7684\u3092\u6c7a\u3081\u305a\u306b `||` \u3092\u4f7f\u3044\u7d9a\u3051\u308b\u3068\u3001\u6b63\u5e38\u306a0\u3084false\u307e\u3067\u5909\u66f4\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<hr>\n<h2>\u300c??\u3068||\u3092\u4e00\u7dd2\u306b\u66f8\u3044\u305f\u3089\u30a8\u30e9\u30fc\u300d\u306e\u76f4\u3057\u65b9<\/h2>\n<p>`??` \u3068 `||`\uff08\u307e\u305f\u306f `&amp;&amp;`\uff09\u3092\u62ec\u5f27\u306a\u3057\u3067\u76f4\u63a5\u6df7\u5728\u3055\u305b\u308b\u3068\u3001JavaScript\u306f SyntaxError \u3092\u51fa\u3057\u307e\u3059\u3002\u6f14\u7b97\u3059\u308b\u9806\u756a\u304c\u66d6\u6627\u306b\u306a\u3089\u306a\u3044\u3088\u3046\u62ec\u5f27\u3067\u610f\u56f3\u3092\u660e\u793a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code>const input = null;\r\nconst valueA = (input ?? 0) || 5;\r\nconst valueB = input ?? (0 || 5);\r\nconsole.log(valueA, valueB); \/\/ 5 5<\/code><\/pre>\n<p>\u3082\u3057 `SyntaxError: Unexpected token` \u306a\u3069\u304c\u51fa\u305f\u3089\u3001\u6700\u521d\u306b\u62ec\u5f27\u3068\u6f14\u7b97\u5b50\u306e\u4e26\u3073\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002`||` \u3092\u6b8b\u3057\u305f\u7d50\u679c\u30010\u3092\u7f6e\u304d\u63db\u3048\u3066\u3044\u306a\u3044\u304b\u3082\u898b\u76f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307e\u305f `unknownVariable ?? 5` \u306e\u3088\u3046\u306b\u5b58\u5728\u3057\u306a\u3044\u5909\u6570\u3092\u76f4\u63a5\u53c2\u7167\u3059\u308b\u3068 ReferenceError \u306b\u306a\u308a\u3001`??` \u3067\u306f\u76f4\u308a\u307e\u305b\u3093\u3002<\/p>\n<h2>\u3069\u3046\u5224\u65ad\u3059\u308b\uff1f\u52d5\u4f5c\u78ba\u8a8d\u306e\u30c1\u30a7\u30c3\u30af<\/h2>\n<ul>\n<li>0\u30fb\u7a7a\u6587\u5b57\u30fbfalse\u3092\u610f\u5473\u306e\u3042\u308b\u5165\u529b\u3068\u3057\u3066\u6b8b\u3059 \u2192 `??` \u3092\u4f7f\u3046<\/li>\n<li>\u7a7a\u6587\u5b57\u30840\u3082\u300c\u7121\u52b9\u300d\u3068\u3057\u3066\u7f6e\u304d\u63db\u3048\u305f\u3044 \u2192 \u6761\u4ef6\u3092\u660e\u793a\u3059\u308b\u3002`||` \u304c\u672c\u5f53\u306b\u610f\u56f3\u3068\u5408\u3046\u304b\u78ba\u8a8d\u3059\u308b<\/li>\n<li>\u53f3\u8fba\u306b\u95a2\u6570\u304c\u3042\u308b \u2192 \u5de6\u8fba\u304c\u63a1\u7528\u3055\u308c\u305f\u3068\u304d\u53f3\u8fba\u306e\u95a2\u6570\u306f\u547c\u3070\u308c\u306a\u3044\u70b9\u306b\u6ce8\u610f\u3059\u308b<\/li>\n<li>\u30a8\u30e9\u30fc\u304c\u51fa\u305f \u2192 \u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u4f8b\u5916\u30e1\u30c3\u30bb\u30fc\u30b8\u3001\u8a72\u5f53\u884c\u3001\u5165\u529b\u5024\u3092\u4e00\u3064\u305a\u3064\u518d\u73fe\u3059\u308b<\/li>\n<\/ul>\n<h3>`??` \u3092\u4f7f\u3048\u3070 `false` \u3082\u65e2\u5b9a\u5024\u306b\u306a\u308a\u307e\u3059\u304b\uff1f<\/h3>\n<p>\u306a\u308a\u307e\u305b\u3093\u3002`false ?? true` \u306f false \u306e\u307e\u307e\u3067\u3059\u3002false\u3092\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u306f\u305d\u306e\u6761\u4ef6\u3092\u660e\u793a\u3057\u307e\u3059\u3002<\/p>\n<h3>`??` \u3068 `||` \u306e\u3069\u3061\u3089\u304c\u65b0\u3057\u3044\u66f8\u304d\u65b9\u3067\u3059\u304b\uff1f<\/h3>\n<p>`??` \u306f null \/ undefined \u7528\u306e\u6f14\u7b97\u5b50\u3067\u3059\u3002\u65b0\u65e7\u3088\u308a\u3001\u3069\u306e\u5024\u3092\u6709\u52b9\u3068\u3057\u3066\u6b8b\u3057\u305f\u3044\u304b\u3067\u9078\u3073\u307e\u3059\u3002<\/p>\n<h2>\u6b21\u306b\u5b66\u3076\u3053\u3068\u30fb\u53c2\u8003\u8cc7\u6599<\/h2>\n<p>\u6b21\u306f `?.`\uff08\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u30c1\u30a7\u30fc\u30f3\uff09\u3092\u5b66\u3076\u3068\u3001\u5024\u304c\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u8aad\u307f\u51fa\u3057\u3068 `??` \u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u7406\u89e3\u3067\u304d\u307e\u3059\u3002`undefined` \u304c\u51fa\u308b\u539f\u56e0\u304b\u3089\u5fa9\u7fd2\u3059\u308b\u5834\u5408\u306f\u95a2\u9023\u3059\u308b\u516c\u958b\u8a18\u4e8b\u3082\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\uff08undefined\u304c\u8868\u793a\u3055\u308c\u308b\u539f\u56e0\u3068\u3001\u30d7\u30ed\u30d1\u30c6\u30a3\u8aad\u307f\u51fa\u3057\u306e\u78ba\u8a8d\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1340\/\">\u306a\u305c\u300cundefined\u300d\u304c\u51fa\u308b\uff1f\u539f\u56e0\u3068\u89e3\u6c7a\u6cd510\u9078<\/a>\u300f\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09<\/p>\n","protected":false},"featured_media":6533,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/6532"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/6533"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=6532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}