Repository navigation
Expand file tree
/
Copy pathjavascript.html
More file actions
1542 lines (1387 loc) · 72 KB
/
Copy pathjavascript.html
File metadata and controls
1542 lines (1387 loc) · 72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Cheatsheet</title>
<style>
:root {
--bg: #f7f8fa;
--panel: #ffffff;
--text: #1c1e21;
--muted: #5b6270;
--border: #e2e5ea;
--accent: #e67e22;
--code-bg: #f0f2f5;
--output-bg: #1c1e21;
--output-fg: #9ef29e;
--output-err: #ff8a8a;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.5;
}
header.page-header { padding: 1.6rem 1.5rem .8rem; text-align: center; }
header.page-header h1 { margin: 0 0 .3rem; font-size: 1.8rem; }
header.page-header p { color: var(--muted); margin: 0 auto; max-width: 640px; }
.sheet-links { margin-top: .6rem; font-size: .85rem; }
.sheet-links a { color: var(--accent); text-decoration: none; margin: 0 .4rem; font-weight: 600; }
.sheet-links a:hover { text-decoration: underline; }
.sheet-links a.current { color: var(--text); text-decoration: underline; }
.live-note {
max-width: 700px; margin: .8rem auto 0; padding: .6rem .9rem;
background: #fff4e8; border: 1px solid #f0cfa0; border-radius: 8px;
font-size: .8rem; color: #7a4a00; text-align: center;
}
.search-bar {
position: sticky; top: 0; z-index: 20;
background: var(--bg);
padding: .8rem 1.5rem;
border-bottom: 1px solid var(--border);
}
.search-bar input {
width: 100%; max-width: 700px; display: block; margin: 0 auto;
padding: .65rem .9rem; font-size: 1rem;
border: 2px solid var(--border); border-radius: 10px;
outline: none; background: var(--panel); color: var(--text);
}
.search-bar input:focus { border-color: var(--accent); }
.search-meta { text-align: center; font-size: .78rem; color: var(--muted); margin-top: .35rem; }
nav.toc {
max-width: 1100px; margin: 1rem auto; padding: .8rem 1.2rem;
background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
}
nav.toc ul { display: flex; flex-wrap: wrap; gap: .5rem; list-style: none; padding: 0; margin: 0; }
nav.toc a {
display: inline-block; padding: .3rem .7rem; border-radius: 999px;
background: var(--code-bg); color: var(--accent); text-decoration: none; font-size: .82rem;
}
nav.toc a:hover { background: var(--accent); color: #fff; }
main { max-width: 1100px; margin: 0 auto 3rem; padding: 0 1.2rem; }
section.category {
background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
margin: 1.4rem 0; padding: 1.1rem 1.2rem;
}
section.category h2 {
margin: 0 0 .3rem; font-size: 1.2rem; display: flex; align-items: baseline; gap: .5rem;
}
section.category .count { font-size: .75rem; font-weight: 400; color: var(--muted); }
section.category .cat-intro {
margin: 0 0 1rem; padding-bottom: .8rem; color: var(--muted); font-size: .85rem;
border-bottom: 2px solid var(--accent);
}
section.category.hidden { display: none; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: .9rem; }
.cheat-card {
border: 1px solid var(--border); border-radius: 10px; padding: .8rem .9rem .9rem; background: #fdfdfe;
display: flex; flex-direction: column;
}
.cheat-card.hidden { display: none; }
.cheat-card h3 {
margin: 0 0 .35rem; font-size: 1rem;
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
color: var(--accent);
}
.cheat-card p.desc { margin: 0 0 .55rem; font-size: .82rem; color: var(--text); opacity: .85; }
.micro-label {
font-size: .65rem; text-transform: uppercase; letter-spacing: .06em;
color: var(--muted); font-weight: 700; margin: 0 0 .25rem;
}
.cheat-card pre {
margin: 0 0 .6rem; background: var(--code-bg); padding: .5rem .6rem; border-radius: 6px;
font-size: .74rem; overflow-x: auto; white-space: pre-wrap; overflow-wrap: anywhere;
}
.cheat-card .preview { font-size: .82rem; margin-top: auto; }
.preview-box {
border: 1px dashed var(--border); border-radius: 6px; padding: .4rem .5rem;
background: #fafbfc; font-size: .8rem;
}
.preview-box.output {
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
background: var(--output-bg); color: var(--output-fg); border: none;
white-space: pre-wrap; overflow-wrap: anywhere; min-height: 1.4em;
}
.preview-box.output.is-error { color: var(--output-err); }
.preview-box.output.is-pending { color: #999; }
code { font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; }
.demo-btn {
font-size: .78rem; padding: .35rem .7rem; border-radius: 6px; border: 1px solid var(--accent);
background: #fff; color: var(--accent); cursor: pointer; margin-bottom: .4rem;
}
.demo-btn:hover { background: var(--accent); color: #fff; }
.demo-list { margin: 0 0 .4rem; padding-left: 1.1em; font-size: .8rem; }
.demo-fragment { border: 1px dashed var(--border); border-radius: 6px; padding: .4rem .5rem; font-size: .78rem; margin-bottom: .4rem; }
.demo-fragment .target { background: #fde9c8; padding: 0 .2rem; border-radius: 3px; }
.demo-fragment#demo-classlist.target { background: #fde9c8; border-color: var(--accent); font-weight: 700; }
#no-results { text-align: center; color: var(--muted); padding: 2rem; display: none; }
footer.page-footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 2rem 0; }
</style>
</head>
<body>
<header class="page-header">
<h1>JavaScript Cheatsheet</h1>
<p>Core JavaScript syntax, built-ins, and browser APIs. Every runnable snippet executes for real in your browser when the page loads, and the actual result is shown below it.</p>
<div class="sheet-links">
<a href="html.html">HTML</a>·
<a href="css.html">CSS</a>·
<a href="javascript.html" class="current">JavaScript</a>·
<a href="angular.html">Angular</a>
</div>
<div class="live-note">⚡ Boxes labeled <strong>Result</strong> are computed live by actually running the code shown above them — not hard-coded text. Interactive cards have real buttons you can click.</div>
</header>
<div class="search-bar">
<input type="text" id="search" placeholder="Search... (e.g. array, promise, map)" autocomplete="off">
<div class="search-meta" id="search-meta"></div>
</div>
<nav class="toc" aria-label="Table of contents">
<ul>
<li><a href="#syntax">Syntax Basics</a></li>
<li><a href="#variables">Variables & Types</a></li>
<li><a href="#operators">Operators</a></li>
<li><a href="#control">Control Flow</a></li>
<li><a href="#functions">Functions</a></li>
<li><a href="#arrays">Arrays</a></li>
<li><a href="#objects">Objects</a></li>
<li><a href="#strings">Strings</a></li>
<li><a href="#classes">Classes</a></li>
<li><a href="#async">Async</a></li>
<li><a href="#dom">DOM & Events</a></li>
<li><a href="#json">JSON & Errors</a></li>
<li><a href="#modules">Modules</a></li>
</ul>
</nav>
<main>
<!-- ===================== SYNTAX BASICS ===================== -->
<section class="category" id="syntax">
<h2>Syntax Basics</h2>
<p class="cat-intro">The minimum punctuation and setup every JS file needs.</p>
<div class="grid">
<div class="cheat-card" data-key="comment single multi line">
<h3>Comments</h3>
<p class="desc">Text ignored by the engine, used to explain code to humans. Single-line comments start with <code>//</code>; multi-line ones are wrapped in <code>/* */</code>.</p>
<div class="micro-label">Code</div>
<pre>// this explains the next line
/* this can
span lines */</pre>
<div class="preview"><div class="preview-box">Comments produce no output — they're stripped before execution.</div></div>
</div>
<div class="cheat-card" data-key="console log print output debug">
<h3>console.log()</h3>
<p class="desc">Prints a value to the browser/DevTools console. It's the most common way to inspect what your code is doing while debugging.</p>
<div class="micro-label">Code</div>
<pre>console.log("Hello, world!");</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">log("Hello, world!");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="semicolon statement terminator">
<h3>Statements & semicolons</h3>
<p class="desc">Each instruction is a statement, conventionally ended with a semicolon. Automatic Semicolon Insertion means they're technically often optional, but omitting them can cause subtle bugs.</p>
<div class="micro-label">Code</div>
<pre>let x = 1;
let y = 2;
x + y;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">let x = 1;
let y = 2;
return x + y;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="script tag embed inline external defer">
<h3>Embedding in HTML</h3>
<p class="desc">JavaScript reaches the browser via a <code><script></code> tag, either inline or pointing at a file. <code>defer</code> delays execution until the HTML is fully parsed.</p>
<div class="micro-label">Code</div>
<pre><script src="app.js" defer></script></pre>
<div class="preview"><div class="preview-box">Not runnable here — this loads an external file.</div></div>
</div>
</div>
</section>
<!-- ===================== VARIABLES & TYPES ===================== -->
<section class="category" id="variables">
<h2>Variables & Types</h2>
<p class="cat-intro">How values are stored, named, and identified.</p>
<div class="grid">
<div class="cheat-card" data-key="let variable mutable block scope">
<h3>let</h3>
<p class="desc">Declares a block-scoped variable that can be reassigned later. Prefer it over <code>var</code> whenever the value needs to change.</p>
<div class="micro-label">Code</div>
<pre>let count = 0;
count = 1;
count;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">let count = 0;
count = 1;
return count;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="const constant immutable binding">
<h3>const</h3>
<p class="desc">Declares a block-scoped variable that cannot be reassigned. The binding is fixed, though objects/arrays assigned to it can still be mutated internally.</p>
<div class="micro-label">Code</div>
<pre>const PI = 3.14;
PI = 4; // throws</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">try {
const PI = 3.14;
PI = 4;
return "no error";
} catch (err) {
return "Error: " + err.message;
}</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="var old function scope hoisting">
<h3>var</h3>
<p class="desc">Older, function-scoped declaration. Its declaration (not its value) is hoisted to the top of the function, so reading it early gives <code>undefined</code> instead of an error.</p>
<div class="micro-label">Code</div>
<pre>console.log(typeof hoisted); // "undefined"
var hoisted = true;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const before = typeof hoisted;
var hoisted = true;
return `before declaration: ${before}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="typeof operator data type check">
<h3>typeof</h3>
<p class="desc">Returns a string naming a value's type. Useful for guarding code that expects a specific kind of value.</p>
<div class="micro-label">Code</div>
<pre>typeof "hi";
typeof 42;
typeof true;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return `${typeof "hi"}, ${typeof 42}, ${typeof true}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="primitive types string number boolean null undefined">
<h3>Primitive types</h3>
<p class="desc">JavaScript has seven primitive value types. Everything else — arrays, functions, dates — is an object under the hood.</p>
<div class="micro-label">Code</div>
<pre>string, number, boolean,
null, undefined, symbol, bigint</pre>
<div class="preview"><div class="preview-box">Primitives are compared and copied by value, not by reference.</div></div>
</div>
<div class="cheat-card" data-key="template literal string interpolation backtick">
<h3>Template literals</h3>
<p class="desc">Backtick-delimited strings that support <code>${expression}</code> interpolation and real line breaks, avoiding manual string concatenation.</p>
<div class="micro-label">Code</div>
<pre>const name = "Ana";
`Hello, ${name}!`</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const name = "Ana";
return `Hello, ${name}!`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="null undefined empty value absence">
<h3>null vs undefined</h3>
<p class="desc"><code>undefined</code> means a variable was never assigned a value; <code>null</code> means a value was explicitly set to "nothing". They're loosely equal but not strictly equal.</p>
<div class="micro-label">Code</div>
<pre>let a;
let b = null;
a == b; // true
a === b; // false</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">let a;
let b = null;
return `a=${a}, b=${b}, a==b: ${a == b}, a===b: ${a === b}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="array destructuring assignment">
<h3>Array destructuring</h3>
<p class="desc">Unpacks array elements into individual named variables in one step, based on their position.</p>
<div class="micro-label">Code</div>
<pre>const [x, y] = [1, 2];</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const [x, y] = [1, 2];
return `x=${x}, y=${y}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="object destructuring assignment">
<h3>Object destructuring</h3>
<p class="desc">Unpacks object properties into variables by matching key names, optionally renaming or defaulting them.</p>
<div class="micro-label">Code</div>
<pre>const { name, age } = { name: "Ana", age: 30 };</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const { name, age } = { name: "Ana", age: 30 };
return `name=${name}, age=${age}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="spread operator expand array object">
<h3>Spread (...)</h3>
<p class="desc">Expands an array or object into its individual elements/properties — handy for copying, merging, or passing many arguments at once.</p>
<div class="micro-label">Code</div>
<pre>const a = [1, 2], b = [3, 4];
const combined = [...a, ...b];</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const a = [1, 2], b = [3, 4];
const combined = [...a, ...b];
return combined.join(", ");</script><div class="preview-box output"></div></div>
</div>
</div>
</section>
<!-- ===================== OPERATORS ===================== -->
<section class="category" id="operators">
<h2>Operators</h2>
<p class="cat-intro">Symbols that combine, compare, or transform values.</p>
<div class="grid">
<div class="cheat-card" data-key="arithmetic operators plus minus multiply divide power modulo">
<h3>Arithmetic</h3>
<p class="desc"><code>+ - * / % **</code> perform addition, subtraction, multiplication, division, remainder, and exponentiation.</p>
<div class="micro-label">Code</div>
<pre>2 ** 3; // power
7 % 2; // remainder</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return `2**3 = ${2 ** 3}, 7%2 = ${7 % 2}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="comparison equal strict triple double equals">
<h3>=== vs ==</h3>
<p class="desc">Strict equality (<code>===</code>) checks type and value with no conversion. Loose equality (<code>==</code>) converts types first, which can produce surprising matches.</p>
<div class="micro-label">Code</div>
<pre>1 === "1";
1 == "1";</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return `1 === "1" is ${1 === "1"}, 1 == "1" is ${1 == "1"}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="logical and or not operators short circuit">
<h3>Logical && || !</h3>
<p class="desc">Combine or negate boolean expressions. <code>&&</code> and <code>||</code> short-circuit, meaning the right side isn't evaluated if the left side already decides the result.</p>
<div class="micro-label">Code</div>
<pre>const isAdmin = true, isActive = false;
isAdmin && isActive;
isAdmin || isActive;
!isActive;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const isAdmin = true, isActive = false;
return `&& : ${isAdmin && isActive}, || : ${isAdmin || isActive}, ! : ${!isActive}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="nullish coalescing double question mark default fallback">
<h3>Nullish coalescing (??)</h3>
<p class="desc">Falls back to the right-hand value only when the left side is <code>null</code> or <code>undefined</code> — unlike <code>||</code>, it keeps falsy values like <code>0</code> or <code>""</code>.</p>
<div class="micro-label">Code</div>
<pre>const config = { port: 0 };
config.port ?? 3000;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const config = { port: 0 };
return `config.port ?? 3000 = ${config.port ?? 3000}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="optional chaining question mark dot safe access">
<h3>Optional chaining (?.)</h3>
<p class="desc">Safely reads a nested property, returning <code>undefined</code> instead of throwing when an intermediate value is missing.</p>
<div class="micro-label">Code</div>
<pre>const user = {};
user?.address?.city;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const user = {};
return String(user?.address?.city);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="ternary conditional operator shorthand if else">
<h3>Ternary (?:)</h3>
<p class="desc">A compact inline conditional: <code>condition ? valueIfTrue : valueIfFalse</code>. Best kept short — nested ternaries hurt readability.</p>
<div class="micro-label">Code</div>
<pre>const isOn = true;
isOn ? "On" : "Off";</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const isOn = true;
return isOn ? "On" : "Off";</script><div class="preview-box output"></div></div>
</div>
</div>
</section>
<!-- ===================== CONTROL FLOW ===================== -->
<section class="category" id="control">
<h2>Control Flow</h2>
<p class="cat-intro">Statements that decide which code runs, and how many times.</p>
<div class="grid">
<div class="cheat-card" data-key="if else conditional branch">
<h3>if / else</h3>
<p class="desc">Runs one block or another depending on whether a condition is truthy. Chain with <code>else if</code> for more branches.</p>
<div class="micro-label">Code</div>
<pre>if (age >= 18) {
"adult";
} else {
"minor";
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const age = 20;
if (age >= 18) {
return "adult";
} else {
return "minor";
}</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="switch case statement multiple branches default">
<h3>switch</h3>
<p class="desc">Matches one value against several <code>case</code> labels — often clearer than a long <code>if/else if</code> chain. Don't forget <code>break</code>.</p>
<div class="micro-label">Code</div>
<pre>switch (day) {
case "Mon": result = "Start of week"; break;
default: result = "Other day";
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const day = "Mon";
let result;
switch (day) {
case "Mon": result = "Start of week"; break;
default: result = "Other day";
}
return result;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="for loop counter iterate classic">
<h3>for</h3>
<p class="desc">The classic counting loop: initializer, condition, and increment expression, each separated by semicolons.</p>
<div class="micro-label">Code</div>
<pre>for (let i = 0; i < 3; i++) {
console.log(i);
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">for (let i = 0; i < 3; i++) {
log(i);
}</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="while loop do while condition repeat">
<h3>while / do...while</h3>
<p class="desc">Repeats a block while a condition holds. <code>while</code> checks before each pass; <code>do...while</code> always runs the body at least once.</p>
<div class="micro-label">Code</div>
<pre>let n = 3, steps = 0;
while (n > 0) { n--; steps++; }</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">let n = 3, steps = 0;
while (n > 0) { n--; steps++; }
return `looped ${steps} times`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="for of loop iterate array values iterable">
<h3>for...of</h3>
<p class="desc">Iterates over the <em>values</em> of any iterable — arrays, strings, Maps, Sets. Usually the right choice for looping over an array's contents.</p>
<div class="micro-label">Code</div>
<pre>for (const item of ["a", "b"]) {
console.log(item);
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">for (const item of ["a", "b"]) {
log(item);
}</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="for in loop iterate object keys enumerable">
<h3>for...in</h3>
<p class="desc">Iterates over the enumerable <em>keys</em> of an object. Not recommended for arrays — use <code>for...of</code> or array methods there instead.</p>
<div class="micro-label">Code</div>
<pre>for (const key in { a: 1, b: 2 }) {
console.log(key);
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">for (const key in { a: 1, b: 2 }) {
log(key);
}</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="break continue loop control skip exit">
<h3>break / continue</h3>
<p class="desc"><code>break</code> exits a loop immediately; <code>continue</code> skips the rest of the current iteration and moves to the next one.</p>
<div class="micro-label">Code</div>
<pre>for (const n of [-1, 5, 200, 3]) {
if (n < 0) continue;
if (n > 100) break;
console.log(n);
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">for (const n of [-1, 5, 200, 3]) {
if (n < 0) continue;
if (n > 100) break;
log(n);
}</script><div class="preview-box output"></div></div>
</div>
</div>
</section>
<!-- ===================== FUNCTIONS ===================== -->
<section class="category" id="functions">
<h2>Functions</h2>
<p class="cat-intro">Reusable blocks of code, and the several ways JavaScript lets you define them.</p>
<div class="grid">
<div class="cheat-card" data-key="function declaration named hoisted">
<h3>Function declaration</h3>
<p class="desc">A named function definition. It's hoisted, so it can be called from code that appears above its definition in the file.</p>
<div class="micro-label">Code</div>
<pre>function add(a, b) {
return a + b;
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">function add(a, b) { return a + b; }
return `add(2, 3) = ${add(2, 3)}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="function expression anonymous assigned variable">
<h3>Function expression</h3>
<p class="desc">A function assigned to a variable. Unlike a declaration, it's not hoisted — it only exists after that line runs.</p>
<div class="micro-label">Code</div>
<pre>const add = function (a, b) {
return a + b;
};</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const add = function (a, b) { return a + b; };
return `add(2, 3) = ${add(2, 3)}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="arrow function lambda concise this binding">
<h3>Arrow function</h3>
<p class="desc">Concise function syntax that doesn't bind its own <code>this</code> — it inherits <code>this</code> from the surrounding scope, which is handy in callbacks.</p>
<div class="micro-label">Code</div>
<pre>const add = (a, b) => a + b;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const add = (a, b) => a + b;
return `add(2, 3) = ${add(2, 3)}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="default parameter value fallback">
<h3>Default parameters</h3>
<p class="desc">Supplies a fallback value used only when the caller omits that argument (or passes <code>undefined</code>).</p>
<div class="micro-label">Code</div>
<pre>function greet(name = "friend") {
return `Hi, ${name}`;
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">function greet(name = "friend") { return `Hi, ${name}`; }
return greet();</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="rest parameter variadic arguments collect">
<h3>Rest parameters</h3>
<p class="desc">Collects any number of remaining arguments into a real array, replacing the older <code>arguments</code> object.</p>
<div class="micro-label">Code</div>
<pre>function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">function sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
return `sum(1, 2, 3) = ${sum(1, 2, 3)}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="closure lexical scope private state remember">
<h3>Closure</h3>
<p class="desc">A function "remembers" the variables from the scope it was created in, even after that scope has finished running. This is how private state is built without classes.</p>
<div class="micro-label">Code</div>
<pre>function counter() {
let n = 0;
return () => ++n;
}
const next = counter();
next(); next(); next();</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">function counter() {
let n = 0;
return () => ++n;
}
const next = counter();
return `${next()}, ${next()}, ${next()}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="callback function argument higher order async">
<h3>Callback function</h3>
<p class="desc">A function passed as an argument so another piece of code can call it later — the basis of event handlers and older async APIs.</p>
<div class="micro-label">Code</div>
<pre>setTimeout(() => {
console.log("done");
}, 800);</pre>
<div class="micro-label">Result — click to run for real</div>
<div class="preview">
<button type="button" class="demo-btn" onclick="demoCallback(this)">Run callback</button>
<div class="preview-box output" id="demo-callback-out">(not run yet)</div>
</div>
</div>
</div>
</section>
<!-- ===================== ARRAYS ===================== -->
<section class="category" id="arrays">
<h2>Arrays</h2>
<p class="cat-intro">Ordered, index-based lists and the methods used to read or transform them.</p>
<div class="grid">
<div class="cheat-card" data-key="array literal create list">
<h3>Array literal</h3>
<p class="desc">The standard way to create an array: a comma-separated list of values inside square brackets.</p>
<div class="micro-label">Code</div>
<pre>const fruits = ["apple", "pear"];</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const fruits = ["apple", "pear"];
return fruits.join(", ");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="push pop add remove end mutate">
<h3>push() / pop()</h3>
<p class="desc">Adds an element to the end of the array, or removes and returns the last element. Both mutate the original array.</p>
<div class="micro-label">Code</div>
<pre>const arr = [1, 2, 3];
arr.push(4);
const removed = arr.pop();</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const arr = [1, 2, 3];
arr.push(4);
log("after push:", arr.join(", "));
const removed = arr.pop();
log("popped:", removed, "-> array:", arr.join(", "));</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="shift unshift add remove start mutate">
<h3>shift() / unshift()</h3>
<p class="desc">Removes/adds an element at the beginning of the array instead of the end. Both mutate the array and are slower than push/pop on large arrays.</p>
<div class="micro-label">Code</div>
<pre>const arr = [1, 2, 3];
arr.unshift(0);
const removed = arr.shift();</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const arr = [1, 2, 3];
arr.unshift(0);
log("after unshift:", arr.join(", "));
const removed = arr.shift();
log("shifted:", removed, "-> array:", arr.join(", "));</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="map transform each element new array non-mutating">
<h3>map()</h3>
<p class="desc">Returns a <em>new</em> array with every element transformed by the given function. The original array is left untouched.</p>
<div class="micro-label">Code</div>
<pre>[1, 2, 3].map(n => n * 2);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [1, 2, 3].map(n => n * 2).join(", ");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="filter select matching elements test">
<h3>filter()</h3>
<p class="desc">Returns a new array containing only the elements for which the test function returns <code>true</code>.</p>
<div class="micro-label">Code</div>
<pre>[1, 2, 3, 4].filter(n => n % 2 === 0);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [1, 2, 3, 4].filter(n => n % 2 === 0).join(", ");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="reduce accumulate fold single value total">
<h3>reduce()</h3>
<p class="desc">Boils an array down to a single value by repeatedly combining an accumulator with each element. Great for sums, totals, or grouping.</p>
<div class="micro-label">Code</div>
<pre>[1, 2, 3].reduce((sum, n) => sum + n, 0);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [1, 2, 3].reduce((sum, n) => sum + n, 0);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="foreach loop each element side effect no return">
<h3>forEach()</h3>
<p class="desc">Runs a function once per element for its side effects. Unlike <code>map()</code>, it always returns <code>undefined</code>.</p>
<div class="micro-label">Code</div>
<pre>[1, 2, 3].forEach(n => console.log(n));</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">[1, 2, 3].forEach(n => log("item:", n));</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="find findindex first match element">
<h3>find() / findIndex()</h3>
<p class="desc">Returns the first element (or its index) that satisfies the test function, or <code>undefined</code>/<code>-1</code> if none match.</p>
<div class="micro-label">Code</div>
<pre>[5, 12, 8].find(n => n > 10);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [5, 12, 8].find(n => n > 10);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="includes contains check exists boolean">
<h3>includes()</h3>
<p class="desc">Checks whether an array contains a given value, returning a plain boolean.</p>
<div class="micro-label">Code</div>
<pre>[1, 2, 3].includes(2);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [1, 2, 3].includes(2);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="sort order alphabetical numeric compare function">
<h3>sort()</h3>
<p class="desc">Sorts the array in place. Without a compare function it sorts as strings, so numbers need an explicit <code>(a, b) => a - b</code> comparator.</p>
<div class="micro-label">Code</div>
<pre>[3, 1, 2].sort((a, b) => a - b);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [3, 1, 2].sort((a, b) => a - b).join(", ");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="slice extract copy portion non-mutating">
<h3>slice()</h3>
<p class="desc">Returns a shallow copy of a portion of the array (start inclusive, end exclusive) without modifying the original.</p>
<div class="micro-label">Code</div>
<pre>[1, 2, 3, 4].slice(1, 3);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return [1, 2, 3, 4].slice(1, 3).join(", ");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="splice insert remove replace mutate in place">
<h3>splice()</h3>
<p class="desc">Adds, removes, or replaces elements in place at a given index, and returns the removed elements.</p>
<div class="micro-label">Code</div>
<pre>const arr = ["a", "b", "c"];
const removed = arr.splice(1, 1, "x", "y");</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const arr = ["a", "b", "c"];
const removed = arr.splice(1, 1, "x", "y");
log("array:", arr.join(", "));
log("removed:", removed.join(", "));</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="join array to string separator">
<h3>join()</h3>
<p class="desc">Concatenates every array element into a single string, separated by the given delimiter (comma by default).</p>
<div class="micro-label">Code</div>
<pre>["a", "b", "c"].join("-");</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return ["a", "b", "c"].join("-");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="array from spread array-like convert isarray">
<h3>Array.from() / Array.isArray()</h3>
<p class="desc"><code>Array.from()</code> builds an array from an iterable or array-like value; <code>Array.isArray()</code> reliably checks whether a value is an array.</p>
<div class="micro-label">Code</div>
<pre>Array.from("abc");
Array.isArray([1]);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return `Array.from("abc") = [${Array.from("abc").join(", ")}], isArray: ${Array.isArray([1])}`;</script><div class="preview-box output"></div></div>
</div>
</div>
</section>
<!-- ===================== OBJECTS ===================== -->
<section class="category" id="objects">
<h2>Objects</h2>
<p class="cat-intro">Unordered key-value collections — JavaScript's core data structure.</p>
<div class="grid">
<div class="cheat-card" data-key="object literal key value pairs create">
<h3>Object literal</h3>
<p class="desc">The standard way to create an object: comma-separated <code>key: value</code> pairs inside curly braces.</p>
<div class="micro-label">Code</div>
<pre>const user = { name: "Ana", age: 30 };</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const user = { name: "Ana", age: 30 };
return JSON.stringify(user);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="dot bracket notation property access read">
<h3>Property access</h3>
<p class="desc">Read or write a property with dot notation (<code>user.name</code>) or bracket notation (<code>user["name"]</code>) — brackets are required when the key is dynamic.</p>
<div class="micro-label">Code</div>
<pre>user.name;
user["name"];</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const user = { name: "Ana" };
return `${user.name} / ${user["name"]}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="object keys values entries iterate list">
<h3>Object.keys() / values() / entries()</h3>
<p class="desc">Returns an array of an object's own keys, values, or <code>[key, value]</code> pairs — the standard way to loop over an object.</p>
<div class="micro-label">Code</div>
<pre>Object.entries({ a: 1, b: 2 });</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return JSON.stringify(Object.entries({ a: 1, b: 2 }));</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="object assign merge copy spread shallow">
<h3>Object.assign() / spread</h3>
<p class="desc">Merges multiple objects into one, or creates a shallow copy. Later sources overwrite earlier ones on matching keys.</p>
<div class="micro-label">Code</div>
<pre>const merged = { ...{ a: 1, b: 2 }, ...{ b: 3, c: 4 } };</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const merged = { ...{ a: 1, b: 2 }, ...{ b: 3, c: 4 } };
return JSON.stringify(merged);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="method shorthand function object literal">
<h3>Method shorthand</h3>
<p class="desc">Lets you define a function as an object property without the <code>function</code> keyword.</p>
<div class="micro-label">Code</div>
<pre>const obj = {
greet() { return "hi"; }
};</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const obj = { greet() { return "hi"; } };
return obj.greet();</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="computed property name bracket dynamic key">
<h3>Computed property names</h3>
<p class="desc">Uses a variable or expression, wrapped in brackets, as the property key while building an object literal.</p>
<div class="micro-label">Code</div>
<pre>const key = "color";
const obj = { [key]: "red" };</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const key = "color";
const obj = { [key]: "red" };
return JSON.stringify(obj);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="map set data structure collection unique keys">
<h3>Map / Set</h3>
<p class="desc"><code>Map</code> is a key-value store that accepts keys of any type; <code>Set</code> stores a collection of unique values with no duplicates.</p>
<div class="micro-label">Code</div>
<pre>const m = new Map([["a", 1]]);
const s = new Set([1, 2, 2]);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const m = new Map([["a", 1]]);
const s = new Set([1, 2, 2]);
return `set size: ${s.size}, map.get("a"): ${m.get("a")}`;</script><div class="preview-box output"></div></div>
</div>
</div>
</section>
<!-- ===================== STRINGS ===================== -->
<section class="category" id="strings">
<h2>Strings</h2>
<p class="cat-intro">Text values and the built-in methods used to inspect or transform them.</p>
<div class="grid">
<div class="cheat-card" data-key="length string size characters">
<h3>.length</h3>
<p class="desc">A property (not a method) holding the number of UTF-16 code units in the string.</p>
<div class="micro-label">Code</div>
<pre>"hello".length;</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return "hello".length;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="touppercase tolowercase case convert">
<h3>toUpperCase() / toLowerCase()</h3>
<p class="desc">Returns a new string with all letters converted to upper or lower case.</p>
<div class="micro-label">Code</div>
<pre>"Hi".toUpperCase();</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return "Hi".toUpperCase();</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="slice substring extract portion string">
<h3>slice()</h3>
<p class="desc">Extracts a portion of a string between two indexes without modifying the original.</p>
<div class="micro-label">Code</div>
<pre>"hello".slice(1, 3);</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return "hello".slice(1, 3);</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="split string to array separator">
<h3>split()</h3>
<p class="desc">Breaks a string into an array of substrings using the given separator.</p>
<div class="micro-label">Code</div>
<pre>"a,b,c".split(",");</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return JSON.stringify("a,b,c".split(","));</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="trim whitespace strip edges">
<h3>trim()</h3>
<p class="desc">Removes whitespace from both ends of a string, leaving interior spaces untouched.</p>
<div class="micro-label">Code</div>
<pre>" hi ".trim();</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return `"${" hi ".trim()}"`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="replace replaceall substring find">
<h3>replace() / replaceAll()</h3>
<p class="desc"><code>replace()</code> swaps the first match; <code>replaceAll()</code> swaps every match. Both accept a string or regular expression.</p>
<div class="micro-label">Code</div>
<pre>"cat".replace("c", "b");</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return "cat".replace("c", "b");</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="includes startswith endswith search substring test">
<h3>includes() / startsWith() / endsWith()</h3>
<p class="desc">Tests whether a substring appears anywhere, at the start, or at the end of the string, returning a boolean.</p>
<div class="micro-label">Code</div>
<pre>"hello".includes("ell");
"hello".startsWith("he");</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">return `includes: ${"hello".includes("ell")}, startsWith: ${"hello".startsWith("he")}`;</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="template literal interpolation multi line expression">
<h3>Template literals</h3>
<p class="desc">Backtick strings can embed any expression, not just variables — including arithmetic, function calls, or ternaries.</p>
<div class="micro-label">Code</div>
<pre>const price = 10, qty = 3;
`Total: ${price * qty}`</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">const price = 10, qty = 3;
return `Total: ${price * qty}`;</script><div class="preview-box output"></div></div>
</div>
</div>
</section>
<!-- ===================== CLASSES ===================== -->
<section class="category" id="classes">
<h2>Classes</h2>
<p class="cat-intro">Syntax for defining object blueprints with shared behavior, built on JavaScript's prototype system.</p>
<div class="grid">
<div class="cheat-card" data-key="class definition constructor method blueprint">
<h3>class</h3>
<p class="desc">Defines a blueprint for creating objects. The <code>constructor</code> runs once per instance to set up its initial state.</p>
<div class="micro-label">Code</div>
<pre>class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound.`; }
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound.`; }
}
return new Animal("Generic").speak();</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="extends inheritance super subclass parent">
<h3>extends / super</h3>
<p class="desc"><code>extends</code> makes one class inherit from another; <code>super</code> calls the parent's constructor or an overridden method.</p>
<div class="micro-label">Code</div>
<pre>class Dog extends Animal {
speak() { return super.speak() + " Woof!"; }
}</pre>
<div class="micro-label">Result</div>
<div class="preview"><script type="text/plain" class="run">class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound.`; }
}
class Dog extends Animal {
speak() { return super.speak() + " Woof!"; }
}
return new Dog("Rex").speak();</script><div class="preview-box output"></div></div>
</div>
<div class="cheat-card" data-key="new instance create object keyword">
<h3>new</h3>
<p class="desc">Creates a new instance of a class, running its constructor and returning a fresh object linked to the class's methods.</p>
<div class="micro-label">Code</div>
<pre>const p = new Point(2, 5);</pre>