{"id":3487,"date":"2024-11-10T09:23:12","date_gmt":"2024-11-10T01:23:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3487\/"},"modified":"2024-11-10T09:23:12","modified_gmt":"2024-11-10T01:23:12","slug":"js%e5%a6%82%e4%bd%95%e9%aa%8c%e8%af%81%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3487\/","title":{"rendered":"js\u5982\u4f55\u9a8c\u8bc1\u6570\u5b57"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u9a8c\u8bc1\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1aisnan() \u51fd\u6570\uff1a\u68c0\u67e5\u503c\u662f\u5426\u4e0d\u662f\u6570\u5b57\uff0c\u5982\u679c\u662f\u8fd4\u56de true\u3002typeof \u64cd\u4f5c\u7b26\uff1a\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\u662f\u5426\u4e3a &#8220;number&#8221;\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528 \/^d+$\/ \u5339\u914d\u6570\u5b57\u683c\u5f0f\u3002number() \u51fd\u6570\uff1a\u5c1d\u8bd5\u5c06\u503c\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u5982\u679c\u6210\u529f\u8fd4\u56de true\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110813331814413.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u9a8c\u8bc1\u6570\u5b57\u63d2\u56fe\" alt=\"js\u5982\u4f55\u9a8c\u8bc1\u6570\u5b57\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u9a8c\u8bc1\u6570\u5b57<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\u9a8c\u8bc1\u6570\u5b57\u975e\u5e38\u7b80\u5355\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. isNaN() \u51fd\u6570<\/strong><\/p>\n<ul>\n<li>isNaN() \u51fd\u6570\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u4e0d\u662f\u6570\u5b57\uff0c\u5982\u679c\u8be5\u503c\u4e0d\u662f\u6570\u5b57\uff0c\u5219\u8fd4\u56de true\uff1b\u5426\u5219\uff0c\u8fd4\u56de false\u3002<\/li>\n<li>\u7528\u6cd5\uff1aisNaN(value)<\/li>\n<\/ul>\n<p><strong>2. typeof \u64cd\u4f5c\u7b26<\/strong><\/p>\n<ul>\n<li>typeof \u64cd\u4f5c\u7b26\u8fd4\u56de\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5982\u679c\u53d8\u91cf\u662f\u6570\u5b57\uff0c\u5219 typeof \u8fd4\u56de &#8220;number&#8221;\u3002<\/li>\n<li>\u7528\u6cd5\uff1atypeof value === &#8220;number&#8221;<\/li>\n<\/ul>\n<p><strong>3. \u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<ul>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1\u6570\u5b57\u683c\u5f0f\u3002\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u6570\u5b57\uff1a<\/li>\n<li>^d+$<\/li>\n<li>\u7528\u6cd5\uff1a\/^d+$\/.test(value)<\/li>\n<\/ul>\n<p><strong>4. Number() \u51fd\u6570<\/strong><\/p>\n<ul>\n<li>Number() \u51fd\u6570\u5c06\u4e00\u4e2a\u503c\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u5982\u679c\u8be5\u503c\u4e0d\u662f\u6570\u5b57\uff0c\u5219\u8fd4\u56de NaN\u3002<\/li>\n<li>\u7528\u6cd5\uff1aNumber(value) !== NaN<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 isNaN() \u51fd\u6570\nconst isNumber1 = isNaN(10); \/\/ false\nconst isNumber2 = isNaN(\"10\"); \/\/ true\n\n\/\/ \u4f7f\u7528 typeof \u64cd\u4f5c\u7b26\nconst isNumber3 = typeof 10 === \"number\"; \/\/ true\nconst isNumber4 = typeof \"10\" === \"number\"; \/\/ false\n\n\/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\nconst isNumber5 = \/^d+$\/.test(\"10\"); \/\/ true\nconst isNumber6 = \/^d+$\/.test(\"10.0\"); \/\/ false\n\n\/\/ \u4f7f\u7528 Number() \u51fd\u6570\nconst isNumber7 = Number(\"10\") !== NaN; \/\/ true\nconst isNumber8 = Number(\"10.0\") !== NaN; \/\/ true<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u9a8c\u8bc1\u6570\u5b57\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u9a8c\u8bc1\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1aisnan() \u51fd\u6570\uff1a\u68c0\u67e5\u503c\u662f\u5426\u4e0d\u662f\u6570\u5b57\uff0c\u5982\u679c\u662f\u8fd4\u56de true\u3002typeof \u64cd\u4f5c\u7b26\uff1a\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\u662f\u5426\u4e3a &#8220;number&#8221;\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528 \/^d+$\/ \u5339\u914d\u6570\u5b57\u683c\u5f0f\u3002number() \u51fd\u6570\uff1a\u5c1d\u8bd5\u5c06\u503c\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u5982\u679c\u6210\u529f\u8fd4\u56de true\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u9a8c\u8bc1\u6570\u5b57 \u5728 JavaScript \u4e2d\u9a8c\u8bc1\u6570\u5b57\u975e\u5e38\u7b80\u5355\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. isNaN() \u51fd\u6570 isNaN() \u51fd\u6570\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u4e0d\u662f\u6570\u5b57\uff0c\u5982\u679c\u8be5\u503c\u4e0d\u662f\u6570\u5b57\uff0c\u5219\u8fd4\u56de true\uff1b\u5426\u5219\uff0c\u8fd4\u56de false\u3002 \u7528\u6cd5\uff1aisNaN(value) 2. typeof \u64cd\u4f5c\u7b26 typeof \u64cd\u4f5c\u7b26\u8fd4\u56de\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5982\u679c\u53d8\u91cf\u662f\u6570\u5b57\uff0c\u5219 typeof \u8fd4\u56de &#8220;number&#8221;\u3002 \u7528\u6cd5\uff1atypeof value === &#8220;number&#8221; 3. \u6b63\u5219\u8868\u8fbe\u5f0f \u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1\u6570\u5b57\u683c\u5f0f\u3002\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u6570\u5b57\uff1a ^d+$ \u7528\u6cd5\uff1a\/^d+$\/.test(value) 4. Number() \u51fd\u6570 Number() \u51fd\u6570\u5c06\u4e00\u4e2a\u503c\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u5982\u679c\u8be5\u503c\u4e0d\u662f\u6570\u5b57\uff0c\u5219\u8fd4\u56de NaN\u3002 \u7528\u6cd5\uff1aNumber(value) !== NaN \u793a\u4f8b\u4ee3\u7801\uff1a \/\/ \u4f7f\u7528 isNaN() [&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-3487","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3487","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=3487"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3487\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}