{"id":2048,"date":"2025-02-25T23:14:14","date_gmt":"2025-02-25T15:14:14","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2048"},"modified":"2025-02-25T23:14:14","modified_gmt":"2025-02-25T15:14:14","slug":"javascript-typeof%e3%80%81null-%e5%92%8c-undefined","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/25\/javascript-typeof%e3%80%81null-%e5%92%8c-undefined\/","title":{"rendered":"JavaScript typeof\u3001null \u548c undefined"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c<code>typeof<\/code> \u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u3002\u800c <code>null<\/code> \u548c <code>undefined<\/code> \u662f\u4e24\u79cd\u4e0d\u540c\u7684\u7279\u6b8a\u503c\uff0c\u7528\u4e8e\u8868\u793a\u4e0d\u540c\u7c7b\u578b\u7684\u201c\u7a7a\u201d\u6216\u201c\u7f3a\u5931\u201d\u72b6\u6001\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>typeof<\/code> \u8fd0\u7b97\u7b26<\/strong><\/h3>\n\n\n\n<p><code>typeof<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u68c0\u6d4b\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u8be5\u503c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>typeof operand;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>operand<\/code>\uff1a\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\u53d8\u91cf\u3001\u5e38\u91cf\u6216\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>typeof<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(typeof \"hello\");       \/\/ \u8f93\u51fa \"string\"\nconsole.log(typeof 123);           \/\/ \u8f93\u51fa \"number\"\nconsole.log(typeof true);          \/\/ \u8f93\u51fa \"boolean\"\nconsole.log(typeof {});            \/\/ \u8f93\u51fa \"object\"\nconsole.log(typeof &#91;]);            \/\/ \u8f93\u51fa \"object\"\nconsole.log(typeof null);          \/\/ \u8f93\u51fa \"object\" (\u7279\u6b8a\u60c5\u51b5)\nconsole.log(typeof undefined);     \/\/ \u8f93\u51fa \"undefined\"<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>typeof<\/code> \u8fd4\u56de\u7684\u662f\u6570\u636e\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>\"hello\"<\/code> \u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c<code>123<\/code> \u662f\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong> <code>typeof null<\/code> \u8fd4\u56de\u7684\u662f <code>\"object\"<\/code>\uff0c\u8fd9\u662f JavaScript \u4e2d\u7684\u4e00\u4e2a\u5386\u53f2\u9057\u7559\u95ee\u9898\uff0c\u5c3d\u7ba1 <code>null<\/code> \u8868\u793a\u201c\u7a7a\u503c\u201d\u6216\u201c\u6ca1\u6709\u5bf9\u8c61\u201d\uff0c\u4f46\u662f <code>typeof null<\/code> \u4ecd\u7136\u8fd4\u56de <code>\"object\"<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>null<\/code><\/strong><\/h3>\n\n\n\n<p><code>null<\/code> \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff0c\u8868\u793a\u201c\u65e0\u503c\u201d\u6216\u201c\u7a7a\u503c\u201d\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u8868\u793a\u53d8\u91cf\u5e94\u8be5\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f46\u6b64\u65f6\u8fd8\u6ca1\u6709\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>null<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let user = null;\nconsole.log(user);  \/\/ \u8f93\u51fa null\n\nif (user === null) {\n    console.log(\"user is null\");\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>null<\/code> \u8868\u793a\u5bf9\u8c61\u7c7b\u578b\u7684\u201c\u7a7a\u5f15\u7528\u201d\uff0c\u5373\u53d8\u91cf\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u5b83\u901a\u5e38\u7528\u4e8e\u663e\u5f0f\u5730\u8bbe\u7f6e\u53d8\u91cf\u4e3a\u7a7a\u503c\uff0c\u6216\u8868\u793a\u201c\u65e0\u6548\u201d\u6216\u201c\u65e0\u5bf9\u8c61\u201d\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. <code>undefined<\/code><\/strong><\/h3>\n\n\n\n<p><code>undefined<\/code> \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff0c\u8868\u793a\u53d8\u91cf\u672a\u88ab\u8d4b\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5f53\u4e00\u4e2a\u53d8\u91cf\u58f0\u660e\u4e86\u4f46\u6ca1\u6709\u8d4b\u503c\u65f6\uff0c\u5b83\u7684\u9ed8\u8ba4\u503c\u662f <code>undefined<\/code>\u3002\u6b64\u5916\uff0c\u5f53\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u5c5e\u6027\u6216\u6570\u7ec4\u7d22\u5f15\u65f6\uff0c\u4e5f\u4f1a\u8fd4\u56de <code>undefined<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>undefined<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let a;\nconsole.log(a);  \/\/ \u8f93\u51fa undefined\uff0c\u56e0\u4e3a\u6ca1\u6709\u8d4b\u503c\n\nlet obj = {};\nconsole.log(obj.someProperty);  \/\/ \u8f93\u51fa undefined\uff0c\u56e0\u4e3a\u6ca1\u6709\u8be5\u5c5e\u6027\n\nlet arr = &#91;1, 2, 3];\nconsole.log(arr&#91;5]);  \/\/ \u8f93\u51fa undefined\uff0c\u56e0\u4e3a\u7d22\u5f15 5 \u4e0d\u5b58\u5728<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>undefined<\/code> \u4ee3\u8868\u4e00\u4e2a\u53d8\u91cf\u5c1a\u672a\u88ab\u8d4b\u503c\uff0c\u6216\u8005\u67d0\u4e2a\u5bf9\u8c61\u6216\u6570\u7ec4\u5143\u7d20\u4e0d\u5b58\u5728\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>null<\/code> \u4e0e <code>undefined<\/code> \u7684\u533a\u522b<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th><code>null<\/code><\/th><th><code>undefined<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>\u5b9a\u4e49<\/strong><\/td><td>\u8868\u793a\u201c\u65e0\u503c\u201d\uff0c\u662f\u4e00\u4e2a\u5bf9\u8c61\u7c7b\u578b\u7684\u7a7a\u5f15\u7528\u3002<\/td><td>\u8868\u793a\u201c\u672a\u5b9a\u4e49\u201d\uff0c\u662f\u53d8\u91cf\u672a\u88ab\u8d4b\u503c\u65f6\u7684\u9ed8\u8ba4\u503c\u3002<\/td><\/tr><tr><td><strong>\u6570\u636e\u7c7b\u578b<\/strong><\/td><td><code>object<\/code>\uff08\u8fd9\u662f\u4e00\u4e2a\u5386\u53f2\u9057\u7559\u95ee\u9898\uff09<\/td><td><code>undefined<\/code><\/td><\/tr><tr><td><strong>\u8d4b\u503c<\/strong><\/td><td>\u53ef\u4ee5\u663e\u5f0f\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5982 <code>let a = null;<\/code><\/td><td>\u53d8\u91cf\u58f0\u660e\u540e\u672a\u8d4b\u503c\u65f6\u81ea\u52a8\u4e3a <code>undefined<\/code><\/td><\/tr><tr><td><strong>\u7528\u9014<\/strong><\/td><td>\u7528\u4e8e\u8868\u793a\u7a7a\u503c\u6216\u5bf9\u8c61\u5f15\u7528\u4e3a\u7a7a\u3002<\/td><td>\u7528\u4e8e\u8868\u793a\u53d8\u91cf\u672a\u8d4b\u503c\u6216\u5c5e\u6027\u4e0d\u5b58\u5728\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>null<\/code> \u548c <code>undefined<\/code> \u7684\u76f8\u7b49\u6027\u6bd4\u8f83<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>null == undefined<\/code> \u4f1a\u8fd4\u56de <code>true<\/code>\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5bbd\u677e\u76f8\u7b49\u6bd4\u8f83\uff08<code>==<\/code>\uff09\u4e2d\u88ab\u8ba4\u4e3a\u662f\u76f8\u7b49\u7684\u3002<\/li>\n\n\n\n<li><code>null === undefined<\/code> \u4f1a\u8fd4\u56de <code>false<\/code>\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u76f8\u7b49\u6027\u6bd4\u8f83<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(null == undefined);   \/\/ \u8f93\u51fa true\nconsole.log(null === undefined);  \/\/ \u8f93\u51fa false<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>typeof<\/code><\/strong>\uff1a\u7528\u6765\u68c0\u67e5\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32\u8868\u793a\u6570\u636e\u7c7b\u578b\u3002\u5bf9\u4e8e <code>null<\/code>\uff0c<code>typeof<\/code> \u8fd4\u56de <code>\"object\"<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a JavaScript \u4e2d\u7684\u7279\u6b8a\u60c5\u51b5\u3002<\/li>\n\n\n\n<li><strong><code>null<\/code><\/strong>\uff1a\u8868\u793a\u201c\u7a7a\u503c\u201d\u6216\u201c\u65e0\u5bf9\u8c61\u5f15\u7528\u201d\u3002\u662f\u4e00\u4e2a\u7c7b\u578b\u4e3a <code>object<\/code> \u7684\u7279\u6b8a\u503c\u3002<\/li>\n\n\n\n<li><strong><code>undefined<\/code><\/strong>\uff1a\u8868\u793a\u53d8\u91cf\u672a\u5b9a\u4e49\u6216\u672a\u8d4b\u503c\u3002\u662f\u53d8\u91cf\u672a\u521d\u59cb\u5316\u65f6\u7684\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5e0c\u671b\u8fd9\u4e9b\u89e3\u91ca\u80fd\u5e2e\u4f60\u7406\u89e3 JavaScript \u4e2d <code>typeof<\/code>\u3001<code>null<\/code> \u548c <code>undefined<\/code> \u7684\u7528\u6cd5\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0ctypeof \u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u3002\u800c null \u548c undefine [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2048","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2048"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2048\/revisions"}],"predecessor-version":[{"id":2049,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2048\/revisions\/2049"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}