{"id":850,"date":"2025-12-22T09:00:38","date_gmt":"2025-12-22T01:00:38","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=850"},"modified":"2025-12-22T09:00:38","modified_gmt":"2025-12-22T01:00:38","slug":"javascript%e4%b8%adundefined%e7%9a%84%e6%ad%a3%e7%a1%ae%e6%89%93%e5%bc%80%e6%96%b9%e5%bc%8f%e8%af%a6%e8%a7%a3-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/22\/javascript%e4%b8%adundefined%e7%9a%84%e6%ad%a3%e7%a1%ae%e6%89%93%e5%bc%80%e6%96%b9%e5%bc%8f%e8%af%a6%e8%a7%a3-2\/","title":{"rendered":"JavaScript\u4e2dundefined\u7684\u6b63\u786e\u6253\u5f00\u65b9\u5f0f\u8be6\u89e3"},"content":{"rendered":"\n<p>JavaScript \u4e2d undefined \u7684\u201c\u6b63\u786e\u6253\u5f00\u65b9\u5f0f\u201d\u8be6\u89e3undefined \u662f JavaScript \u4e2d\u6700\u5bb9\u6613\u5f15\u8d77\u56f0\u60d1\u548c bug \u7684\u539f\u59cb\u503c\u4e4b\u4e00\u3002<br>\u5b83\u8868\u793a**\u201c\u503c\u4e0d\u5b58\u5728\u201d\u6216\u201c\u5c1a\u672a\u5b9a\u4e49\u201d**\uff0c\u4f46\u53c8\u4e0d\u662f null\uff08null \u662f\u201c\u660e\u786e\u8868\u793a\u6ca1\u6709\u503c\u201d\uff09\u3002\u4e0b\u9762\u4ece\u672c\u8d28\u3001\u5e38\u89c1\u573a\u666f\u3001\u68c0\u67e5\u65b9\u5f0f\u3001\u6700\u4f73\u5b9e\u8df5\u3001\u5e38\u89c1\u9677\u9631\u5230\u73b0\u4ee3\u89e3\u51b3\u65b9\u6848\u5168\u9762\u8bb2\u89e3\u30021. undefined \u7684\u672c\u8d28<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u7279\u6027<\/th><th>\u8bf4\u660e<\/th><\/tr><tr><td>\u7c7b\u578b<\/td><td>typeof undefined === &#8216;undefined&#8217;<\/td><\/tr><tr><td>\u503c<\/td><td>\u552f\u4e00\u503c\uff1aundefined<\/td><\/tr><tr><td>\u662f\u5426\u53ef\u91cd\u65b0\u8d4b\u503c\uff08ES5 \u524d\uff09<\/td><td>\u662f\uff08\u5168\u5c40 undefined \u53ef\u88ab\u8986\u76d6\uff0c\u5982 undefined = 42\uff09<\/td><\/tr><tr><td>ES5 \u540e<\/td><td>\u5168\u5c40 undefined \u662f\u4e0d\u53ef\u5199\u3001\u4e0d\u53ef\u914d\u7f6e\u7684\u5c5e\u6027\uff0c\u4f46\u53ef\u88ab\u906e\u853d\uff08shadow\uff09<\/td><\/tr><tr><td>\u4e0e null \u7684\u533a\u522b<\/td><td>null == undefined \u2192 true null === undefined \u2192 false<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7ed3\u8bba\uff1a\u73b0\u4ee3 JS\uff08ES5+\uff09\u4e2d\uff0c\u5168\u5c40 undefined \u662f\u5b89\u5168\u7684\uff0c\u4f46\u4e0d\u8981\u5728\u4f5c\u7528\u57df\u5185\u7528 undefined \u505a\u53d8\u91cf\u540d\u30022. undefined \u51fa\u73b0\u7684 6 \u5927\u7ecf\u5178\u573a\u666f<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u58f0\u660e\u4f46\u672a\u8d4b\u503c<br>let x; console.log(x); \/\/ undefined<\/li>\n\n\n\n<li>\u51fd\u6570\u53c2\u6570\u672a\u4f20<br>function fn(a) { console.log(a); } fn(); \/\/ undefined<\/li>\n\n\n\n<li>\u5bf9\u8c61\u5c5e\u6027\u4e0d\u5b58\u5728<br>const obj = {}; console.log(obj.foo); \/\/ undefined<\/li>\n\n\n\n<li>\u51fd\u6570\u65e0\u8fd4\u56de\u503c<br>function fn() { } console.log(fn()); \/\/ undefined<\/li>\n\n\n\n<li>\u6570\u7ec4\u8d8a\u754c<br>const arr = [1,2]; console.log(arr[10]); \/\/ undefined<\/li>\n\n\n\n<li>\u89e3\u6784\u8d4b\u503c\u5931\u8d25<br>const { name } = {}; console.log(name); \/\/ undefined<\/li>\n<\/ol>\n\n\n\n<p>3. \u68c0\u67e5 undefined \u7684 5 \u79cd\u65b9\u5f0f\uff08\u53ca\u63a8\u8350\u5ea6\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u65b9\u5f0f<\/th><th>\u4ee3\u7801\u793a\u4f8b<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u63a8\u8350\u5ea6<\/th><th>\u8bf4\u660e<\/th><\/tr><tr><td>=== undefined<\/td><td>if (x === undefined)<\/td><td>\u53d8\u91cf\u5df2\u58f0\u660e\uff0c\u68c0\u67e5\u662f\u5426\u8d4b\u503c<\/td><td>\u2605\u2605\u2605\u2605\u2605<\/td><td>\u6700\u5e38\u7528\u3001\u6700\u6e05\u6670<\/td><\/tr><tr><td>typeof x === &#8216;undefined&#8217;<\/td><td>if (typeof x === &#8216;undefined&#8217;)<\/td><td>\u53d8\u91cf\u53ef\u80fd\u672a\u58f0\u660e\uff08\u5982\u5168\u5c40\u53d8\u91cf\uff09<\/td><td>\u2605\u2605\u2605\u2605<\/td><td>\u9632 ReferenceError<\/td><\/tr><tr><td>x == null<\/td><td>if (x == null)<\/td><td>\u540c\u65f6\u68c0\u67e5 null \u548c undefined<\/td><td>\u2605\u2605\u2605<\/td><td>\u5e38\u7528\u4e8e\u201c\u7a7a\u503c\u201d\u573a\u666f<\/td><\/tr><tr><td>x === void 0<\/td><td>if (x === void 0)<\/td><td>\u6781\u5ea6\u8ffd\u6c42\u5b89\u5168\uff08\u9632 undefined \u88ab\u8986\u76d6\uff09<\/td><td>\u2605\u2605<\/td><td>\u5386\u53f2\u9057\u7559\uff0c\u73b0\u4ee3\u57fa\u672c\u4e0d\u7528<\/td><\/tr><tr><td>!x \u6216 x<\/td><td>if (!x)<\/td><td>\u4ec5\u5f53 undefined\/null \u90fd\u89c6\u4e3a\u201c\u5047\u201d<\/td><td>\u2605\u2605<\/td><td>\u5371\u9669\uff01\u4f1a\u8bef\u5224 0\u3001&#8221;\u3001false<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u73b0\u4ee3\u63a8\u8350\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>99% \u573a\u666f\u7528 === undefined<\/li>\n\n\n\n<li>\u6781\u5c11\u6570\u573a\u666f\uff08\u68c0\u67e5\u5168\u5c40\u672a\u58f0\u660e\u53d8\u91cf\uff09\u7528 typeof &#8230; === &#8216;undefined&#8217;<\/li>\n<\/ul>\n\n\n\n<p>4. \u6700\u4f73\u5b9e\u8df5\uff082025 \u5e74\u6807\u51c6\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350\u5199\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><tr><td>\u51fd\u6570\u53c2\u6570\u9ed8\u8ba4\u503c<\/td><td>function fn(x = 100) {}<\/td><td>ES6 \u9ed8\u8ba4\u53c2\u6570<\/td><\/tr><tr><td>\u5bf9\u8c61\u5c5e\u6027\u5b89\u5168\u8bbf\u95ee<\/td><td>obj?.prop ?? &#8216;\u9ed8\u8ba4\u503c&#8217;<\/td><td>\u7ed3\u5408\u53ef\u9009\u94fe + nullish<\/td><\/tr><tr><td>\u6df1\u5c42\u5c5e\u6027\u8bbf\u95ee<\/td><td>user?.profile?.avatar?.url ?? &#8216;default.png&#8217;<\/td><td>\u9632\u62a5\u9519<\/td><\/tr><tr><td>\u68c0\u67e5\u662f\u5426\u5b9a\u4e49<\/td><td>if (typeof window.someGlobal === &#8216;undefined&#8217;)<\/td><td>\u6d4f\u89c8\u5668\u73af\u5883\u5168\u5c40\u53d8\u91cf<\/td><\/tr><tr><td>\u540c\u65f6\u5904\u7406 null\/undefined<\/td><td>value ?? defaultValue<\/td><td>\u4f18\u5148\u7528 nullish \u5408\u5e76<\/td><\/tr><tr><td>\u907f\u514d\u8bef\u5224 falsy \u503c<\/td><td>\u4e0d\u8981\u7528 !value \u6216 `value<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>5. \u5e38\u89c1\u9677\u9631 &amp; \u9519\u8bef\u5199\u6cd5\uff08\u907f\u5751\u6307\u5357\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u9519\u8bef\u5199\u6cd5<\/th><th>\u95ee\u9898<\/th><th>\u6b63\u786e\u5199\u6cd5<\/th><\/tr><tr><td>if (x == undefined)<\/td><td>\u4f1a\u628a null \u4e5f\u5f53\u6210 true<\/td><td>x === undefined<\/td><\/tr><tr><td>if (!x)<\/td><td>\u628a 0\u3001&#8221;\u3001false \u4e5f\u5f53\u6210 undefined<\/td><td>x === undefined<\/td><\/tr><tr><td>if (x === &#8216;undefined&#8217;)<\/td><td>\u6bd4\u8f83\u7684\u662f\u5b57\u7b26\u4e32\uff0c\u4e0d\u662f\u539f\u59cb\u503c<\/td><td>typeof x === &#8216;undefined&#8217;<\/td><\/tr><tr><td>\u5728\u4f5c\u7528\u57df\u5185 let undefined = 123;<\/td><td>\u906e\u853d\u5168\u5c40 undefined\uff0c\u540e\u7eed x === undefined \u5931\u6548<\/td><td>\u6c38\u8fdc\u4e0d\u8981\u8fd9\u4e48\u5e72<\/td><\/tr><tr><td>obj.prop === undefined<\/td><td>\u65e0\u6cd5\u533a\u5206\u201c\u5c5e\u6027\u4e0d\u5b58\u5728\u201d vs \u201c\u5c5e\u6027\u503c\u4e3a undefined\u201d<\/td><td>\u7528 prop in obj \u6216 obj.hasOwnProperty(&#8216;prop&#8217;)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>6. \u73b0\u4ee3\u5229\u5668\uff08ES2020+\uff09\uff1a\u5f7b\u5e95\u89e3\u653e undefined \u70e6\u607c<\/p>\n\n\n\n<p>javascript<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u53ef\u9009\u94fe + nullish \u5408\u5e76\uff08\u6700\u5f3a\u7ec4\u5408\uff09\nconst name = user?.info?.name ?? &#039;\u533f\u540d\u7528\u6237&#039;;\n\n\/\/ \u5b89\u5168\u8c03\u7528\u65b9\u6cd5\nconst result = obj?.method?.() ?? &#039;\u9ed8\u8ba4\u7ed3\u679c&#039;;\n\n\/\/ \u89e3\u6784 + \u9ed8\u8ba4\u503c\nconst { age = 18 } = person ?? {};\n\n\/\/ \u94fe\u5f0f + \u63d0\u4f9b\u9ed8\u8ba4\nconst street = data?.user?.address?.street ?? &#039;\u672a\u77e5\u8857\u9053&#039;;\n<\/pre><\/div>\n\n\n<p>7. \u603b\u7ed3\uff1aundefined \u7684\u201c\u6b63\u786e\u6253\u5f00\u65b9\u5f0f\u201d\u53e3\u8bc0<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u58f0\u660e\u4e86\u6ca1\u8d4b\u503c \u2192 undefined\n\u51fd\u6570\u6ca1\u8fd4\u56de\u503c \u2192 undefined\n\u5c5e\u6027\u4e0d\u5b58\u5728 \u2192 undefined\n\n\u68c0\u67e5\u65b9\u5f0f\uff08\u4f18\u5148\u7ea7\uff09\uff1a\n1. x === undefined           \/\/ \u6700\u5e38\u7528\n2. typeof x === &#039;undefined&#039;  \/\/ \u9632\u672a\u58f0\u660e\n3. x ?? default              \/\/ \u73b0\u4ee3\u9996\u9009\uff08\u5904\u7406 null\/undefined\uff09\n4. obj?.prop                 \/\/ \u9632\u94fe\u5f0f\u62a5\u9519\n\n\u6c38\u8fdc\u4e0d\u8981\uff1a\n- \u7528 undefined \u505a\u53d8\u91cf\u540d\n- \u7528 == undefined\n- \u7528 !x \u6765\u5224\u65ad undefined\n<\/pre><\/div>\n\n\n<p>\u8bb0\u4f4f\u4e00\u53e5\u8bdd\uff1a<br>\u201cundefined \u662f\u610f\u5916\u7684\u5ba2\u4eba\uff0cnull \u662f\u4f60\u8bf7\u6765\u7684\u5ba2\u4eba\u3002\u201d<br>\u7528 === undefined \u548c ?? \u628a\u610f\u5916\u7684\u5ba2\u4eba\u6321\u5728\u95e8\u5916\u3002\u5982\u679c\u6709\u5177\u4f53\u4ee3\u7801\u573a\u666f\u9700\u8981\u5e2e\u4f60\u4f18\u5316\u9632 undefined\uff0c\u6b22\u8fce\u8d34\u51fa\u6765\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d undefined \u7684\u201c\u6b63\u786e\u6253\u5f00\u65b9\u5f0f\u201d\u8be6\u89e3undefi&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/22\/javascript%e4%b8%adundefined%e7%9a%84%e6%ad%a3%e7%a1%ae%e6%89%93%e5%bc%80%e6%96%b9%e5%bc%8f%e8%af%a6%e8%a7%a3-2\/\">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-850","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\/850","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=850"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/850\/revisions"}],"predecessor-version":[{"id":851,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/850\/revisions\/851"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}