{"id":518,"date":"2025-12-05T19:29:40","date_gmt":"2025-12-05T11:29:40","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=518"},"modified":"2025-12-05T19:29:40","modified_gmt":"2025-12-05T11:29:40","slug":"js%e4%b8%ad%e7%a9%ba%e5%80%bc%e5%90%88%e5%b9%b6%e8%bf%90%e7%ae%97%e7%ac%a6-%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/js%e4%b8%ad%e7%a9%ba%e5%80%bc%e5%90%88%e5%b9%b6%e8%bf%90%e7%ae%97%e7%ac%a6-%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"JS\u4e2d\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26 ?? \u7684\u4f7f\u7528"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u8d85\u6e05\u6670\u3001\u6700\u65b0\u3001\u5b9e\u6218\u7ea7\u522b<\/strong> \u7684<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u300aJavaScript \u4e2d\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26 ?? \u4f7f\u7528\u8be6\u89e3\u300b<\/h1>\n\n\n\n<p>\u8fd9\u662f ES2020 \u5f15\u5165\u7684\u65b0\u7279\u6027\uff0c\u7528\u8d77\u6765\u6bd4 <code>||<\/code> \u66f4\u5b89\u5168\u3001\u66f4\u806a\u660e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e00\u3001\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26 <code>??<\/code> \u662f\u4ec0\u4e48\uff1f<\/h1>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na ?? b\n\n<\/pre><\/div>\n\n\n<p>\u542b\u4e49\uff1a<\/p>\n\n\n\n<p>\ud83d\udc49 <strong>\u5f53 a \u4e3a null \u6216 undefined \u65f6\uff0c\u8fd4\u56de b\uff1b\u5426\u5219\u8fd4\u56de a\u3002<\/strong><\/p>\n\n\n\n<p>\u6362\u53e5\u8bdd\u8bf4\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u503c<\/th><th>\u7ed3\u679c\u662f\u5426\u5224\u5b9a\u4e3a\u7a7a\uff1f<\/th><\/tr><\/thead><tbody><tr><td>null<\/td><td>\u662f\uff08\u7a7a\uff09<\/td><\/tr><tr><td>undefined<\/td><td>\u662f\uff08\u7a7a\uff09<\/td><\/tr><tr><td>0<\/td><td>\u274c \u5426<\/td><\/tr><tr><td>&#8220;&#8221;<\/td><td>\u274c \u5426<\/td><\/tr><tr><td>false<\/td><td>\u274c \u5426<\/td><\/tr><tr><td>NaN<\/td><td>\u274c \u5426<\/td><\/tr><tr><td>\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u51fd\u6570<\/td><td>\u274c \u5426<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf \u4e8c\u3001\u5b83\u548c <code>||<\/code> \u7684\u5173\u952e\u533a\u522b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n|| \u4f1a\u628a 0 &quot;&quot; false \u90fd\u5f53\u4f5c\u7a7a\n?? \u53ea\u4f1a\u628a null \u548c undefined \u5f53\u4f5c\u7a7a\n\n<\/pre><\/div>\n\n\n<p>\u5bf9\u6bd4\u4f8b\u5b50 \ud83d\udc47<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 ||<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst n1 = 0 || 5;\nconsole.log(n1); \/\/ 5\uff08\u56e0\u4e3a 0 \u88ab\u5f53\u6210\u201c\u5047\u201d\uff09\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 ??<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst n2 = 0 ?? 5;\nconsole.log(n2); \/\/ 0\uff08\u4fdd\u7559\u771f\u5b9e 0\uff09\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u5c31\u662f <code>??<\/code> \u7684\u6700\u5927\u4ef7\u503c\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e09\u3001\u5e38\u7528\u573a\u666f\uff08\u8d85\u5b9e\u7528\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2b50 \u573a\u666f 1\uff1a\u63a5\u53e3\uff08API\uff09\u8fd4\u56de\u503c\u53ef\u80fd\u4e3a null<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst user = apiResponse.user ?? {};\n\n<\/pre><\/div>\n\n\n<p>\u63a5\u53e3\u5982\u679c\u8fd4\u56de\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>user = null \u2192 \u8fd4\u56de {}<\/li>\n\n\n\n<li>user = undefined \u2192 \u8fd4\u56de {}<\/li>\n\n\n\n<li>user = {} \u2192 \u8fd4\u56de {}<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2b50 \u573a\u666f 2\uff1a\u8868\u5355\u9ed8\u8ba4\u503c<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst age = form.age ?? 18;\n\n<\/pre><\/div>\n\n\n<p>\u5373\u4f7f\u7528\u6237\u8f93\u5165 age=0\uff08\u5408\u6cd5\u5e74\u9f84\uff09\u4e5f\u4e0d\u4f1a\u88ab\u66ff\u6362\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2b50 \u573a\u666f 3\uff1a\u5bf9\u8c61\u5c5e\u6027\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst width = config.width ?? 300;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2b50 \u573a\u666f 4\uff1a\u77ed\u8def\u8d4b\u503c\uff08\u63a8\u8350\u5199\u6cd5\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u66ff\u4ee3\u65e7\u5199\u6cd5\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlet a = a !== null &amp;amp;&amp;amp; a !== undefined ? a : 10;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u65b0\u5199\u6cd5\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = a ?? 10;\n\n<\/pre><\/div>\n\n\n<p>\u66f4\u5e72\u51c0\u3001\u53ef\u8bfb\u6027\u66f4\u9ad8\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2b50 \u573a\u666f 5\uff1a\u4ece localStorage \u83b7\u53d6\u6570\u636e\u65f6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst theme = localStorage.getItem(&quot;theme&quot;) ?? &quot;light&quot;;\n\n<\/pre><\/div>\n\n\n<p>\u5982\u679c\u4e3b\u9898\u503c\u4e3a\u7a7a\u5b57\u7b26\u4e32 <code>\"dark\"<\/code> \u2192 \u4e0d\u88ab\u8986\u76d6<br>\u5982\u679c\u4e3a <code>null<\/code> \u2192 \u8fd4\u56de <code>light<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2b50 \u573a\u666f 6\uff1a\u51fd\u6570\u53c2\u6570\u9ed8\u8ba4\u503c<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction greet(name) {\n  name = name ?? &quot;\u6e38\u5ba2&quot;;\n  console.log(&quot;\u4f60\u597d\uff0c&quot; + name);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udeab \u56db\u3001<code>??<\/code> \u4e0d\u80fd\u4e0e <code>||<\/code>\u3001<code>&amp;&amp;<\/code> \u6df7\u7528\uff08\u5fc5\u987b\u52a0\u62ec\u53f7\uff09<\/h1>\n\n\n\n<p>\u4e0b\u9762\u5199\u6cd5\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na || b ?? c\n\n<\/pre><\/div>\n\n\n<p>\u5fc5\u987b\u5199\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na || (b ?? c)\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf \u4e94\u3001\u7ec8\u6781\u603b\u7ed3\uff08\u9002\u5408\u80cc\u8bf5\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fd0\u7b97\u7b26<\/th><th>\u5224\u65ad\u7a7a\u7684\u8303\u56f4<\/th><th>\u5e38\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td>`<\/td><td><\/td><td>`<\/td><\/tr><tr><td><code>??<\/code><\/td><td>\u4ec5 null\u3001undefined<\/td><td>\u66f4\u51c6\u786e\u7684\u9ed8\u8ba4\u503c\u8bbe\u7f6e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u63a8\u8350\uff1a\u53ea\u8981\u5e0c\u671b\u4fdd\u7559\u6709\u6548\u7684 0 &#8220;&#8221; false \u2192 \u7528 ??<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u8d85\u6e05\u6670\u3001\u6700\u65b0\u3001\u5b9e\u6218\u7ea7\u522b \u7684 \u300aJavaScript \u4e2d\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/js%e4%b8%ad%e7%a9%ba%e5%80%bc%e5%90%88%e5%b9%b6%e8%bf%90%e7%ae%97%e7%ac%a6-%e7%9a%84%e4%bd%bf%e7%94%a8\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/518\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}