{"id":168310,"date":"2024-05-08T16:21:23","date_gmt":"2024-05-08T08:21:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168310.html"},"modified":"2024-05-08T16:21:28","modified_gmt":"2024-05-08T08:21:28","slug":"javascript-%e4%b8%ad-%e5%92%8c-%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168310.html","title":{"rendered":"Javascript \u4e2d == \u548c === \u533a\u522b\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034444\/2ecf08a5-eddf-477d-809f-364c2ddac419.webp\" alt=\"Javascript \u4e2d == \u548c === \u533a\u522b\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>\u7b49\u53f7\u201c==\u201d\u548c\u5168\u7b49\u53f7\u201c===\u201d\u5728JavaScript\u4e2d\u662f\u7528\u6765\u8fdb\u884c\u6bd4\u8f83\u64cd\u4f5c\u7684\u8fd0\u7b97\u7b26\u3002<strong>\u7b49\u53f7\u201c==\u201d\u662f\u6267\u884c\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u540e\u518d\u6bd4\u8f83\u503c\u662f\u5426\u76f8\u540c\uff0c\u800c\u5168\u7b49\u53f7\u201c===\u201d\u4e0d\u4ec5\u6bd4\u8f83\u503c\u8fd8\u8981\u6bd4\u8f83\u4e24\u8005\u7684\u7c7b\u578b\u662f\u5426\u4e00\u81f4<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u6bd4\u8f83\u7684\u53d8\u91cf\u7c7b\u578b\u4e0d\u540c\uff0c\u201c==\u201d\u53ef\u80fd\u4f1a\u8fd4\u56detrue\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5c1d\u8bd5\u7c7b\u578b\u8f6c\u6362\u6765\u8fdb\u884c\u6bd4\u8f83\uff1b\u4f46\u662f\uff0c\u201c===\u201d\u5219\u4f1a\u8fd4\u56defalse\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u201c==\u201d\u65f6\uff0c\u5982\u679c\u4e24\u4e2a\u53d8\u91cf\u7c7b\u578b\u4e0d\u540c\uff0cJavaScript \u4f1a\u5c06\u5b83\u4eec\u8f6c\u6362\u6210\u4e00\u4e2a\u5171\u540c\u7684\u7c7b\u578b\u518d\u8fdb\u884c\u6bd4\u8f83\uff0c\u800c\u8fd9\u79cd\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\u79f0\u4e3a\u7c7b\u578b\u5f3a\u5236(type coercion)\u3002\u4f8b\u5982\uff0c\u6bd4\u8f83\u6570\u503c\u548c\u5b57\u7b26\u4e32\u65f6\uff0c\u5b57\u7b26\u4e32\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6570\u503c\uff0c\u7136\u540e\u8fdb\u884c\u6570\u503c\u7684\u6bd4\u8f83\uff1b\u6bd4\u8f83\u5e03\u5c14\u503c\u548c\u975e\u5e03\u5c14\u503c\u65f6\uff0c\u975e\u5e03\u5c14\u503c\u5c06\u88ab\u8f6c\u6362\u6210\u5e03\u5c14\u503c\uff0c\u7136\u540e\u8fdb\u884c\u6bd4\u8f83\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u503c\u7c7b\u578b\u4e0e\u5f15\u7528\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u6709\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff1a\u539f\u59cb\u6570\u636e\u7c7b\u578b\uff08\u5982\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u3001undefined\u3001null\u548cSymbol\uff09\u548c\u5bf9\u8c61\uff08\u5305\u62ec\u6570\u7ec4\u548c\u51fd\u6570\uff09\u3002\u539f\u59cb\u6570\u636e\u7c7b\u578b\u50a8\u5b58\u5728\u6808\u5185\u5b58\u4e2d\uff0c\u6bcf\u4e2a\u53d8\u91cf\u76f4\u63a5\u5305\u542b\u5b83\u7684\u503c\uff0c\u800c\u5bf9\u8c61\u50a8\u5b58\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u53d8\u91cf\u4e2d\u50a8\u5b58\u7684\u662f\u4e00\u4e2a\u6307\u5411\u5806\u5185\u5b58\u4e2d\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u4e3a\u4e86\u7406\u89e3\u201c==\u201d\u548c\u201c===\u201d\u7684\u533a\u522b\uff0c\u9996\u5148\u9700\u8981\u77e5\u9053JavaScript\u4e2d\u7684\u8fd9\u4e24\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>JavaScript\u5728\u4f7f\u7528\u201c==\u201d\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u5982\u679c\u53d8\u91cf\u7c7b\u578b\u4e0d\u540c\uff0c\u4f1a\u6839\u636e\u62bd\u8c61\u76f8\u7b49\u6bd4\u8f83\u7b97\u6cd5\u5c06\u4e00\u65b9\u6216\u53cc\u65b9\u8f6c\u6362\u4e3a\u76f8\u540c\u7c7b\u578b\u7136\u540e\u518d\u6bd4\u8f83\u3002\u8fd9\u6837\u7684\u8f6c\u6362\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5bfc\u81f4\u6df7\u6dc6\u548c\u9884\u6599\u4e4b\u5916\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5f53\u6bd4\u8f830\u548c&quot;0&quot;\u65f6\uff0c\u5c3d\u7ba1\u4e00\u4e2a\u662f\u6570\u5b57\u800c\u53e6\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u201c==\u201d\u5f97\u5230\u7684\u7ed3\u679c\u662ftrue\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u201c0\u201d\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6570\u5b570\u7136\u540e\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e25\u683c\u76f8\u7b49\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5168\u7b49\u53f7\u201c===\u201d\u6267\u884c\u4e25\u683c\u6bd4\u8f83\uff0c\u4e0d\u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u53ea\u6709\u5f53\u4e24\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u76f8\u540c\u5e76\u4e14\u503c\u76f8\u7b49\u65f6\uff0c\u624d\u4f1a\u8fd4\u56detrue\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u201c==\u201d\u7684\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u53ef\u80fd\u5e26\u6765\u7684\u95ee\u9898\u3002\u5982<code>0 === &quot;0&quot;<\/code>\u5c06\u8fd4\u56defalse\uff0c\u56e0\u4e3a\u867d\u7136\u503c\u770b\u8d77\u6765\u76f8\u4f3c\uff0c\u4f46\u5b83\u4eec\u7684\u7c7b\u578b\u4e0d\u540c\uff1a\u4e00\u4e2a\u662f\u6570\u5b57\uff0c\u53e6\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u793a\u4f8b\u4e0e\u63a8\u8350<\/h3>\n<\/p>\n<p><p>\u4e3e\u4f8b\u6765\u8bb2\uff1a<code>false == 0<\/code> \u4f1a\u8fd4\u56detrue\uff0c\u56e0\u4e3a\u6839\u636e\u7c7b\u578b\u5f3a\u5236\u89c4\u5219\uff0cfalse\u4f1a\u88ab\u8f6c\u6362\u4e3a0\u540e\u8fdb\u884c\u6bd4\u8f83\uff1b\u53cd\u4e4b\uff0c<code>false === 0<\/code> \u5219\u4f1a\u8fd4\u56defalse\uff0c\u56e0\u4e3a\u5b83\u4eec\u7c7b\u578b\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u63a8\u8350\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u201c===\u201d\u6765\u907f\u514d\u53ef\u80fd\u7684\u8bef\u89e3\u6216\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u5728\u65e5\u5e38\u7f16\u7801\u4e2d\uff0c\u5efa\u8bae\u5c3d\u53ef\u80fd\u4f7f\u7528\u201c===\u201d\u8fdb\u884c\u6bd4\u8f83\uff0c\u56e0\u4e3a\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u6e05\u6670\u5ea6\u3002\u5c3d\u7ba1\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u201c==\u201d\u7684\u7c7b\u578b\u5f3a\u5236\u53ef\u4ee5\u5e26\u6765\u4fbf\u5229\uff0c\u4f46\u5b83\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u96be\u4ee5\u9884\u6599\uff0c\u56e0\u6b64\u5728\u6ca1\u6709\u5145\u5206\u7406\u89e3\u5176\u80cc\u540e\u7684\u8f6c\u6362\u89c4\u5219\u4e4b\u524d\uff0c\u4f7f\u7528\u5b83\u53ef\u80fd\u5bfc\u81f4bug\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u8003\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u65b9\u9762\uff0c\u7531\u4e8e\u201c===\u201d\u4e0d\u9700\u8981\u989d\u5916\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u6240\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5b83\u7684\u6027\u80fd\u8981\u4f18\u4e8e\u201c==\u201d\u3002\u4f46\u901a\u5e38\u8fd9\u79cd\u6027\u80fd\u5dee\u5f02\u975e\u5e38\u5fae\u5c0f\uff0c\u4e0d\u592a\u53ef\u80fd\u5bf9\u73b0\u4ee3web\u5e94\u7528\u4ea7\u751f\u663e\u8457\u5f71\u54cd\u3002\u7136\u800c\uff0c\u4ece\u7f16\u7801\u98ce\u683c\u548c\u51c6\u786e\u6027\u89d2\u5ea6\u51fa\u53d1\uff0c\u201c===\u201d\u662f\u66f4\u4f73\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7b80\u800c\u8a00\u4e4b\uff0c\u4e86\u89e3\u201c==\u201d\u548c\u201c===\u201d\u4e4b\u95f4\u7684\u533a\u522b\u5bf9\u4e8e\u5199\u51fa\u53ef\u9760\u4e14\u6613\u7ef4\u62a4\u7684JavaScript\u4ee3\u7801\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u867d\u7136\u201c==\u201d\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f7f\u7528\u8d77\u6765\u66f4\u7075\u6d3b\uff0c\u4f46\u5b83\u53ef\u80fd\u4f1a\u5f15\u5165\u610f\u6599\u4e4b\u5916\u7684\u884c\u4e3a\uff0c\u800c\u201c===\u201d\u56e0\u4e3a\u5176\u4e25\u683c\u6027\uff0c\u786e\u4fdd\u4e86\u6bd4\u8f83\u7684\u4e25\u8c28\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u638c\u63e1\u4f55\u65f6\u4f7f\u7528\u201c==\u201d\u548c\u4f55\u65f6\u575a\u6301\u4f7f\u7528\u201c===\u201d\u80fd\u591f\u5e2e\u4f60\u7f16\u5199\u51fa\u66f4\u52a0\u7a33\u5b9a\u548c\u7b26\u5408\u9884\u671f\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: Javascript \u4e2d\u7684 == \u548c === \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<p>A: Javascript \u4e2d\u7684 == \u548c === \u662f\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u503c\u7684\u7b49\u5f0f\u3002<\/p>\n<ul>\n<li><strong>==<\/strong> \u8fd0\u7b97\u7b26\u6267\u884c\u677e\u6563\u76f8\u7b49\u6bd4\u8f83\uff0c\u5373\u5728\u6bd4\u8f83\u4e24\u4e2a\u503c\u4e4b\u524d\uff0c\u4f1a\u5148\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u5982\u679c\u4e24\u4e2a\u503c\u7684\u7c7b\u578b\u4e0d\u540c\uff0c== \u8fd0\u7b97\u7b26\u4f1a\u5c1d\u8bd5\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u76f8\u540c\u7c7b\u578b\uff0c\u7136\u540e\u518d\u6bd4\u8f83\u5b83\u4eec\u7684\u503c\u3002\u4f46\u662f\uff0c\u5728\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c== \u8fd0\u7b97\u7b26\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u5916\u7684\u8f6c\u6362\u7ed3\u679c\uff0c\u4ece\u800c\u5bfc\u81f4\u6bd4\u8f83\u7ed3\u679c\u4e0d\u7b26\u5408\u9884\u671f\u3002<\/li>\n<li><strong>===<\/strong> \u8fd0\u7b97\u7b26\u6267\u884c\u4e25\u683c\u76f8\u7b49\u6bd4\u8f83\uff0c\u5b83\u4e0d\u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u5728\u4f7f\u7528 === \u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u4e0d\u4ec5\u8981\u6bd4\u8f83\u503c\u662f\u5426\u76f8\u540c\uff0c\u8fd8\u8981\u6bd4\u8f83\u503c\u7684\u7c7b\u578b\u662f\u5426\u76f8\u540c\u3002\u53ea\u6709\u5f53\u4e24\u4e2a\u503c\u7684\u7c7b\u578b\u548c\u503c\u90fd\u76f8\u7b49\u65f6\uff0c=== \u8fd0\u7b97\u7b26\u624d\u4f1a\u8fd4\u56de true\u3002<\/li>\n<\/ul>\n<p><strong>Q: \u5982\u4f55\u5728 Javascript \u4e2d\u9009\u62e9\u6b63\u786e\u7684\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff1f<\/strong><\/p>\n<p>A: \u5728 Javascript \u4e2d\u9009\u62e9\u6b63\u786e\u7684\u76f8\u7b49\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u8fd0\u7b97\u7b26\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u4f60\u5e0c\u671b\u6bd4\u8f83\u4e24\u4e2a\u503c\u7684\u503c\u662f\u5426\u76f8\u7b49\uff0c\u5e76\u4e14\u4e0d\u5173\u5fc3\u5b83\u4eec\u7684\u7c7b\u578b\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u3002<\/li>\n<li>\u5982\u679c\u4f60\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u503c\u7684\u7c7b\u578b\u548c\u503c\u662f\u5426\u5b8c\u5168\u76f8\u540c\uff0c\u90a3\u4e48\u5e94\u8be5\u4f7f\u7528 === \u8fd0\u7b97\u7b26\u3002<\/li>\n<\/ul>\n<p><strong>Q: \u4f7f\u7528 == \u548c === \u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>A: \u5728\u4f7f\u7528 == \u548c === \u8fdb\u884c\u76f8\u7b49\u6bd4\u8f83\u65f6\uff0c\u6709\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u8003\u8651\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u65f6\uff0c\u8981\u6ce8\u610f\u7c7b\u578b\u8f6c\u6362\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u6bd4\u8f83\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32 &quot;1&quot; \u548c\u6570\u5b57 1\uff0c\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de true\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32 &quot;1&quot; \u4f1a\u88ab\u8f6c\u6362\u4e3a\u6570\u5b57 1\u3002<\/li>\n<li>\u4f7f\u7528 === \u8fd0\u7b97\u7b26\u65f6\uff0c\u8981\u786e\u4fdd\u6bd4\u8f83\u7684\u503c\u7684\u7c7b\u578b\u548c\u503c\u90fd\u76f8\u540c\u3002\u5982\u679c\u7c7b\u578b\u6216\u503c\u6709\u4e00\u4e2a\u4e0d\u540c\uff0c=== \u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de false\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32 &quot;1&quot; \u548c\u6570\u5b57 1\uff0c\u4f7f\u7528 === \u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de false\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u7b49\u53f7\u201c==\u201d\u548c\u5168\u7b49\u53f7\u201c===\u201d\u5728JavaScript\u4e2d\u662f\u7528\u6765\u8fdb\u884c\u6bd4\u8f83\u64cd\u4f5c\u7684\u8fd0\u7b97\u7b26\u3002\u7b49\u53f7\u201c==\u201d\u662f\u6267\u884c\u81ea\u52a8\u7c7b\u578b\u8f6c [&hellip;]","protected":false},"author":3,"featured_media":168315,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168310"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=168310"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168310\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168315"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}