{"id":15024,"date":"2024-09-30T23:03:09","date_gmt":"2024-09-30T15:03:09","guid":{"rendered":"https:\/\/fgchen.com\/wpedu\/?p=15024"},"modified":"2026-03-30T14:29:28","modified_gmt":"2026-03-30T06:29:28","slug":"javascript-cheat-sheet","status":"publish","type":"post","link":"https:\/\/fgchen.com\/wpedu\/2024\/09\/javascript-cheat-sheet\/","title":{"rendered":"JavaScript Cheat Sheet"},"content":{"rendered":"<h2>JavaScript Cheat Sheet<\/h2>\n<h3>1. <strong>\u8b8a\u6578\u8207\u5e38\u91cf<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let name = &#039;Alice&#039;;  \/\/ \u53ef\u4ee5\u6539\u8b8a\nconst age = 25;      \/\/ \u5e38\u91cf\uff0c\u4e0d\u53ef\u6539\u8b8a\nvar isStudent = true; \/\/ \u5168\u57df\u8b8a\u6578<\/code><\/pre>\n<h3>2. <strong>\u8cc7\u6599\u578b\u5225<\/strong><\/h3>\n<ul>\n<li><code>String<\/code>: <code>&#039;Hello&#039;<\/code><\/li>\n<li><code>Number<\/code>: <code>42<\/code><\/li>\n<li><code>Boolean<\/code>: <code>true<\/code> or <code>false<\/code><\/li>\n<li><code>Array<\/code>: <code>[1, 2, 3]<\/code><\/li>\n<li><code>Object<\/code>: <code>{name: &#039;Alice&#039;, age: 25}<\/code><\/li>\n<li><code>Null<\/code>: <code>null<\/code><\/li>\n<li><code>Undefined<\/code>: <code>undefined<\/code><\/li>\n<\/ul>\n<h3>3. <strong>\u904b\u7b97\u7b26<\/strong><\/h3>\n<ul>\n<li><strong>\u6578\u5b78\u904b\u7b97<\/strong>: <code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code>, <code>%<\/code> (\u53d6\u9918)<\/li>\n<li><strong>\u905e\u589e\/\u905e\u6e1b<\/strong>: <code>++<\/code>, <code>--<\/code><\/li>\n<li><strong>\u8ce6\u503c<\/strong>: <code>=<\/code>, <code>+=<\/code>, <code>-=<\/code>, <code>*=<\/code>, <code>\/=<\/code><\/li>\n<li><strong>\u6bd4\u8f03<\/strong>: <code>==<\/code>, <code>===<\/code>, <code>!=<\/code>, <code>!==<\/code>, <code>&lt;<\/code>, <code>&gt;<\/code>, <code>&lt;=<\/code>, <code>&gt;=<\/code><\/li>\n<li><strong>\u908f\u8f2f\u904b\u7b97<\/strong>: <code>&amp;&amp;<\/code> (\u4e14), <code>||<\/code> (\u6216), <code>!<\/code> (\u975e)<\/li>\n<\/ul>\n<h3>4. <strong>\u689d\u4ef6\u5224\u65b7<\/strong><\/h3>\n<pre><code class=\"language-javascript\">if (condition) {\n    \/\/ do something\n} else if (anotherCondition) {\n    \/\/ do something else\n} else {\n    \/\/ fallback\n}<\/code><\/pre>\n<h3>5. <strong>\u8ff4\u5708<\/strong><\/h3>\n<pre><code class=\"language-javascript\">\/\/ for \u8ff4\u5708\nfor (let i = 0; i &lt; 10; i++) {\n    console.log(i);\n}\n\n\/\/ while \u8ff4\u5708\nlet i = 0;\nwhile (i &lt; 10) {\n    console.log(i);\n    i++;\n}<\/code><\/pre>\n<h3>6. <strong>\u51fd\u6578<\/strong><\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u5b9a\u7fa9\u51fd\u6578\nfunction greet(name) {\n    return &#039;Hello, &#039; + name;\n}\n\n\/\/ \u7bad\u982d\u51fd\u6578 (Arrow Function)\nconst greet = (name) =&gt; &#039;Hello, &#039; + name;<\/code><\/pre>\n<h3>7. <strong>\u9663\u5217<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let fruits = [&#039;Apple&#039;, &#039;Banana&#039;, &#039;Cherry&#039;];\n\n\/\/ \u8a2a\u554f\u5143\u7d20\nconsole.log(fruits[0]); \/\/ &#039;Apple&#039;\n\n\/\/ \u65b0\u589e\u5143\u7d20\nfruits.push(&#039;Mango&#039;);\n\n\/\/ \u9663\u5217\u9577\u5ea6\nconsole.log(fruits.length); \/\/ 4\n\n\/\/ \u8fed\u4ee3\u9663\u5217\nfruits.forEach(function(fruit) {\n    console.log(fruit);\n});<\/code><\/pre>\n<h3>8. <strong>\u7269\u4ef6 (Object)<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let person = {\n    name: &#039;Alice&#039;,\n    age: 25,\n    isStudent: true\n};\n\n\/\/ \u8a2a\u554f\u5c6c\u6027\nconsole.log(person.name); \/\/ &#039;Alice&#039;\n\n\/\/ \u4fee\u6539\u5c6c\u6027\nperson.age = 26;<\/code><\/pre>\n<h3>9. <strong>DOM \u64cd\u4f5c<\/strong><\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u7372\u53d6\u5143\u7d20\nlet element = document.getElementById(&#039;myElement&#039;);\nlet elements = document.querySelectorAll(&#039;.myClass&#039;);\n\n\/\/ \u4fee\u6539\u5167\u5bb9\nelement.textContent = &#039;\u65b0\u7684\u5167\u5bb9&#039;;\n\n\/\/ \u65b0\u589e\u4e8b\u4ef6\u76e3\u807d\nelement.addEventListener(&#039;click&#039;, function() {\n    alert(&#039;\u5143\u7d20\u88ab\u9ede\u64ca\u4e86\uff01&#039;);\n});<\/code><\/pre>\n<h3>10. <strong>\u4e8b\u4ef6\u8655\u7406<\/strong><\/h3>\n<pre><code class=\"language-javascript\">document.getElementById(&#039;btn&#039;).addEventListener(&#039;click&#039;, function() {\n    alert(&#039;Button clicked&#039;);\n});<\/code><\/pre>\n<h3>11. <strong>JSON<\/strong><\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u5c07\u7269\u4ef6\u8f49\u63db\u6210 JSON \u5b57\u4e32\nlet jsonString = JSON.stringify(person);\n\n\/\/ \u5c07 JSON \u5b57\u4e32\u89e3\u6790\u6210\u7269\u4ef6\nlet jsonObject = JSON.parse(jsonString);<\/code><\/pre>\n<h3>12. <strong>\u932f\u8aa4\u8655\u7406<\/strong><\/h3>\n<pre><code class=\"language-javascript\">try {\n    \/\/ \u53ef\u80fd\u51fa\u932f\u7684\u7a0b\u5f0f\u78bc\n    let result = riskyFunction();\n} catch (error) {\n    console.error(error);\n} finally {\n    \/\/ \u7121\u8ad6\u5982\u4f55\u90fd\u6703\u57f7\u884c\n    console.log(&#039;Done&#039;);\n}<\/code><\/pre>\n<h3>13. <strong>\u65e5\u671f\u8207\u6642\u9593<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let now = new Date();\nconsole.log(now); \/\/ \u7576\u524d\u6642\u9593<\/code><\/pre>\n<h3>14. <strong>\u689d\u4ef6 (\u4e09\u5143\u904b\u7b97\u7b26)<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let age = 20;\nlet canVote = (age &gt;= 18) ? &#039;Yes&#039; : &#039;No&#039;;<\/code><\/pre>\n<h3>15. <strong>\u9663\u5217\u65b9\u6cd5<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let numbers = [1, 2, 3, 4, 5];\n\n\/\/ Map\nlet doubled = numbers.map(n =&gt; n * 2);\n\n\/\/ Filter\nlet evens = numbers.filter(n =&gt; n % 2 === 0);\n\n\/\/ Reduce\nlet sum = numbers.reduce((acc, n) =&gt; acc + n, 0);<\/code><\/pre>\n<h3>16. <strong>\u6a21\u677f\u5b57\u4e32<\/strong><\/h3>\n<pre><code class=\"language-javascript\">let name = &#039;Alice&#039;;\nlet greeting = `Hello, ${name}!`; \/\/ \u4f7f\u7528\u53cd\u5f15\u865f ``<\/code><\/pre>\n<h3>17. <strong>ES6 \u6a21\u7d44\u5316 (import\/export)<\/strong><\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u5b9a\u7fa9\nexport function greet() {\n    console.log(&#039;Hello!&#039;);\n}\n\n\/\/ \u4f7f\u7528\nimport { greet } from &#039;.\/greet.js&#039;;\ngreet();<\/code><\/pre>\n<p>\u9019\u5f35 Cheat Sheet \u6db5\u84cb\u4e86 JavaScript \u4e2d\u6700\u5e38\u7528\u7684\u8a9e\u6cd5\u548c\u529f\u80fd\uff0c\u9069\u5408\u5feb\u901f\u67e5\u95b1\u548c\u53c3\u8003\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Cheat Sheet 1. \u8b8a\u6578\u8207\u5e38\u91cf let name &hellip; <\/p>\n","protected":false},"author":1,"featured_media":14977,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[266],"tags":[],"class_list":["post-15024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-266"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/15024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=15024"}],"version-history":[{"count":2,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/15024\/revisions"}],"predecessor-version":[{"id":15169,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/15024\/revisions\/15169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media\/14977"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=15024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/categories?post=15024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/tags?post=15024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}