{"id":52821,"date":"2024-12-03T08:29:56","date_gmt":"2024-12-03T00:29:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52821\/"},"modified":"2024-12-03T08:29:56","modified_gmt":"2024-12-03T00:29:56","slug":"javascript-%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%90%88%e5%b9%b6%e8%bf%90%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52821\/","title":{"rendered":"JavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??`"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??`<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??`\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323582046741b03c4219c.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??`\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??`\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u672c\u535a\u5ba2\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 javascript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26 <strong>??<\/strong>\u3002\u6211\u4eec\u5c06\u5b83\u4e0e\u903b\u8f91 or \u8fd0\u7b97\u7b26 <strong>||<\/strong> \u8fdb\u884c\u6bd4\u8f83\uff0c\u8ba8\u8bba\u5176\u4f18\u5148\u7ea7\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u793a\u4f8b\u6765\u5e2e\u52a9\u60a8\u7406\u89e3\u5176\u7528\u6cd5\u3002\u8ba9\u6211\u4eec\u6f5c\u5165\u5427\uff01<\/p>\n<h3> <strong>\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26??<\/strong> <\/h3>\n<p>\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26 <strong>??<\/strong> \u7528\u4e8e\u5728\u5904\u7406 <strong>null<\/strong> \u6216 <strong>undefined<\/strong> \u65f6\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5f53\u5de6\u4fa7\u64cd\u4f5c\u6570\u4e3a <strong>null<\/strong> \u6216 <strong>undefined<\/strong> \u65f6\uff0c\u5b83\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u3002\u5426\u5219\uff0c\u5b83\u8fd4\u56de\u5de6\u4fa7\u64cd\u4f5c\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>result = value1 ?? value2;\n\n<\/pre>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let user = null;\nlet defaultuser = \"guest\";\n\nlet currentuser = user ?? defaultuser;\nconsole.log(currentuser); \/\/ output: \"guest\"\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u7528\u6237<\/strong>&nbsp;\u4e3a<strong>\u7a7a<\/strong>\u3002<\/li>\n<li><strong>??<\/strong> \u8fd0\u7b97\u7b26\u8fd4\u56de <strong>defaultuser<\/strong>\uff0c\u56e0\u4e3a <strong>user<\/strong> \u4e3a <strong>null<\/strong>\u3002<\/li>\n<\/ul>\n<h3> <strong>\u4e0e&nbsp;||<\/strong> \u6bd4\u8f83 <\/h3>\n<p>\u903b\u8f91\u6216\u8fd0\u7b97\u7b26<strong>||<\/strong>\u8fd4\u56de\u5b83\u9047\u5230\u7684\u7b2c\u4e00\u4e2a\u771f\u503c\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u5de6\u64cd\u4f5c\u6570\u4e3a\u5047\uff0c\u5b83\u5c06\u8fd4\u56de\u53f3\u64cd\u4f5c\u6570 (<strong>false<\/strong>, <strong>0<\/strong>, <strong>&#8220;&#8221;<\/strong>, <strong>null<\/strong>\u3001<strong>\u672a\u5b9a\u4e49<\/strong>\u3001<strong>nan<\/strong>\uff09\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let user = \"\";\nlet defaultuser = \"guest\";\n\nlet currentuser = user || defaultuser;\nconsole.log(currentuser); \/\/ output: \"guest\"\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>user<\/strong>&nbsp;\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32&nbsp;<strong>&#8220;&#8221;<\/strong>\uff08\u5047\uff09\u3002<\/li>\n<li><strong>||<\/strong> \u8fd0\u7b97\u7b26\u8fd4\u56de <strong>defaultuser<\/strong>\uff0c\u56e0\u4e3a <strong>user<\/strong> \u4e3a\u5047\u3002<\/li>\n<\/ul>\n<h3> <strong>\u4e3b\u8981\u5dee\u5f02<\/strong> <\/h3>\n<ul>\n<li> <strong>??<\/strong>\uff1a\u4ec5\u5c06 <strong>null<\/strong> \u548c <strong>\u672a\u5b9a\u4e49<\/strong> \u89c6\u4e3a\u865a\u5047\u503c\u3002<\/li>\n<li> <strong>||<\/strong>\uff1a\u8003\u8651\u6240\u6709\u865a\u5047\u503c (<strong>false<\/strong>,&nbsp;<strong>0<\/strong>,&nbsp;<strong>&#8220;&#8221;<\/strong>,&nbsp;<strong>null<\/strong>,&nbsp;<strong>\u672a\u5b9a\u4e49<\/strong>\uff0c<strong>nan<\/strong>\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let user = 0;\nlet defaultuser = \"guest\";\n\nlet currentuser1 = user ?? defaultuser;\nlet currentuser2 = user || defaultuser;\n\nconsole.log(currentuser1); \/\/ output: 0\nconsole.log(currentuser2); \/\/ output: \"guest\"\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u7528\u6237<\/strong> \u662f <strong>0<\/strong>\uff08\u5bf9\u4e8e <strong>||<\/strong> \u4e3a\u5047\uff0c\u4f46\u5bf9\u4e8e <strong>??<\/strong> \u5219\u4e0d\u7136\uff09\u3002<\/li>\n<li><strong>??<\/strong>&nbsp;\u8fd0\u7b97\u7b26\u8fd4\u56de <strong>0<\/strong>\uff0c\u56e0\u4e3a <strong>user<\/strong> \u4e0d\u4e3a&nbsp;<strong>null<\/strong>&nbsp;\u6216 <strong>\u672a\u5b9a\u4e49<\/strong>\u3002<\/li>\n<li><strong>||<\/strong>&nbsp;\u8fd0\u7b97\u7b26\u8fd4\u56de<strong>\u201cguest\u201d<\/strong>\uff0c\u56e0\u4e3a<strong>\u7528\u6237<\/strong>\u662f\u5047\u7684\u3002<\/li>\n<\/ul>\n<h3> <strong>\u4f18\u5148\u7ea7<\/strong> <\/h3>\n<p>\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26 <strong>??<\/strong> \u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u5927\u591a\u6570\u5176\u4ed6\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec\u903b\u8f91 or \u8fd0\u7b97\u7b26 <strong>||<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u6d89\u53ca <strong>??<\/strong> \u7684\u8868\u8fbe\u5f0f\u5728\u6d89\u53ca <strong>||<\/strong>.<\/p>\n<p> \u7684\u8868\u8fbe\u5f0f\u4e4b\u540e\u6c42\u503c <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let a = null;\nlet b = false;\nlet c = \"hello\";\n\nlet result1 = a ?? b || c;\nlet result2 = a || b ?? c;\n\nconsole.log(result1); \/\/ output: \"hello\"\nconsole.log(result2); \/\/ output: \"hello\"\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u4e00\u4e2a?? b<\/strong>&nbsp;\u8fd4\u56de <strong>false<\/strong>\uff0c\u56e0\u4e3a <strong>a<\/strong> \u4e3a <strong>null<\/strong>\uff0c\u5e76\u4e14 <strong>b<\/strong>&nbsp;\u4e3a <strong>false<\/strong>\u3002<\/li>\n<li> <strong>\u5047|| c<\/strong>&nbsp;\u8fd4\u56de <strong>\u201chello\u201d<\/strong>\uff0c\u56e0\u4e3a <strong>false<\/strong> \u4e3a\u5047\uff0c\u800c <strong>c<\/strong> \u4e3a\u771f\u3002<\/li>\n<li> <strong>\u4e00\u4e2a|| b<\/strong>&nbsp;\u8fd4\u56de <strong>false<\/strong>\uff0c\u56e0\u4e3a <strong>a<\/strong> \u4e3a <strong>null<\/strong>\uff0c\u5e76\u4e14 <strong>b<\/strong>&nbsp;\u4e3a <strong>false<\/strong>\u3002<\/li>\n<li> <strong>\u5047\u7684\uff1f\uff1f c<\/strong>&nbsp;\u8fd4\u56de<strong>\u201chello\u201d<\/strong>\uff0c\u56e0\u4e3a<strong>false<\/strong>&nbsp;\u4e0d\u662f<strong>null<\/strong>\u6216<strong>\u672a\u5b9a\u4e49<\/strong>\u3002<\/li>\n<\/ul>\n<h3> <strong>\u5b9e\u9645\u4f8b\u5b50<\/strong> <\/h3>\n<p>\u8ba9\u6211\u4eec\u7528\u4e00\u4e9b\u5b9e\u9645\u4f8b\u5b50\u5c06\u6240\u6709\u5185\u5bb9\u653e\u5728\u4e00\u8d77\uff1a<\/p>\n<h3> <strong>\u4f7f\u7528??&nbsp;\u5bf9\u4e8e\u9ed8\u8ba4\u503c<\/strong> <\/h3>\n<pre>function getusername(user) {\n  return user.name ?? \"guest\";\n}\n\nlet user1 = { name: \"alice\" };\nlet user2 = { age: 25 };\n\nconsole.log(getusername(user1)); \/\/ output: \"alice\"\nconsole.log(getusername(user2)); \/\/ output: \"guest\"\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c<strong>user.name<\/strong>&nbsp;\u4e3a<strong>null<\/strong>\u6216<strong>\u672a\u5b9a\u4e49<\/strong>\uff0c\u5219<strong>??<\/strong>\u64cd\u4f5c\u7b26\u8fd4\u56de<strong>\u201cguest\u201d<\/strong> .<\/li>\n<\/ul>\n<h3> <strong>\u4f7f\u7528||&nbsp;\u5bf9\u4e8e\u9ed8\u8ba4\u503c<\/strong> <\/h3>\n<pre>function getUserName(user) {\n  return user.name || \"Guest\";\n}\n\nlet user1 = { name: \"\" };\nlet user2 = { age: 25 };\n\nconsole.log(getUserName(user1)); \/\/ Output: \"Guest\"\nconsole.log(getUserName(user2)); \/\/ Output: \"Guest\"\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c <strong>user.name<\/strong> \u4e3a\u5047\uff08\u4f8b\u5982\uff0c<strong>&#8220;&#8221;<\/strong>\uff09\uff0c\u5219 <strong>||<\/strong> \u8fd0\u7b97\u7b26\u8fd4\u56de <strong>\u201cguest\u201d<\/strong>\u3002<\/li>\n<\/ul>\n<h3> <strong>\u603b\u7ed3<\/strong> <\/h3>\n<ul>\n<li> <strong>??&nbsp;\uff08\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26\uff09<\/strong>\uff1a\u5982\u679c\u5de6\u4fa7\u64cd\u4f5c\u6570\u4e3a <strong>null<\/strong> \u6216 <strong>\u672a\u5b9a\u4e49<\/strong>\uff0c\u5219\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u3002<\/li>\n<li> <strong>||&nbsp;\uff08\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\uff09<\/strong>\uff1a\u8fd4\u56de\u9047\u5230\u7684\u7b2c\u4e00\u4e2a\u771f\u503c\u3002<\/li>\n<li> <strong>\u4f18\u5148\u7ea7<\/strong>\uff1a<strong>??<\/strong> \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e <strong>||<\/strong> \u8fd0\u7b97\u7b26\u3002<\/li>\n<\/ul>\n<h3> <strong>\u7ed3\u8bba<\/strong> <\/h3>\n<p>\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26 <strong>??<\/strong> \u662f javascript \u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u5904\u7406 <strong>null<\/strong> \u6216 <strong>undefined<\/strong> \u65f6\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u901a\u8fc7\u4e86\u89e3\u5b83\u4e0e\u903b\u8f91 or \u8fd0\u7b97\u7b26 <strong>||<\/strong> \u7684\u533a\u522b\u53ca\u5176\u4f18\u5148\u7ea7\uff0c\u60a8\u5c06\u80fd\u591f\u7f16\u5199\u66f4\u5065\u58ee\u4e14\u65e0\u9519\u8bef\u7684\u4ee3\u7801\u3002\u4e0d\u65ad\u7ec3\u4e60\u548c\u63a2\u7d22\uff0c\u52a0\u6df1\u5bf9 javascript \u4e2d nullish \u5408\u5e76\u8fd0\u7b97\u7b26\u7684\u7406\u89e3\u3002<\/p>\n<p>\u8bf7\u7ee7\u7eed\u5173\u6ce8\u6709\u5173 javascript \u7684\u66f4\u591a\u6df1\u5165\u535a\u5ba2\uff01\u5feb\u4e50\u7f16\u7801\uff01<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   HTML\u4e2dRuby\u6807\u7b7e\u4e0b\u5212\u7ebf\u51fa\u73b0\u95f4\u9694\u600e\u4e48\u529e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??` \u6536\u85cf \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aJavaScript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26`??`\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u5728\u672c\u535a\u5ba2\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 javascript \u4e2d\u7684\u7a7a\u5408\u5e76\u8fd0\u7b97\u7b26 ??\u3002\u6211\u4eec\u5c06\u5b83\u4e0e\u903b\u8f91 or \u8fd0\u7b97\u7b26 || \u8fdb\u884c\u6bd4\u8f83\uff0c\u8ba8\u8bba\u5176\u4f18\u5148\u7ea7\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u793a\u4f8b\u6765\u5e2e\u52a9\u60a8\u7406\u89e3\u5176\u7528\u6cd5\u3002\u8ba9\u6211\u4eec\u6f5c\u5165\u5427\uff01 \u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26?? \u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26 ?? \u7528\u4e8e\u5728\u5904\u7406 null \u6216 undefined \u65f6\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5f53\u5de6\u4fa7\u64cd\u4f5c\u6570\u4e3a null \u6216 undefined \u65f6\uff0c\u5b83\u8fd4\u56de\u53f3\u4fa7\u64cd\u4f5c\u6570\u3002\u5426\u5219\uff0c\u5b83\u8fd4\u56de\u5de6\u4fa7\u64cd\u4f5c\u6570\u3002 \u8bed\u6cd5\uff1a result = value1 ?? value2; \u793a\u4f8b\uff1a let user = null; let defaultuser = &#8220;guest&#8221;; let currentuser = user ?? defaultuser; console.log(currentuser); \/\/ output: &#8220;guest&#8221; \u8bf4\u660e\uff1a \u7528\u6237&nbsp;\u4e3a\u7a7a\u3002 ?? \u8fd0\u7b97\u7b26\u8fd4\u56de defaultuser\uff0c\u56e0\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52821","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52821"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52821\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}