-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript-interview-guide.html
More file actions
1637 lines (1603 loc) · 70.7 KB
/
javascript-interview-guide.html
File metadata and controls
1637 lines (1603 loc) · 70.7 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 Interview Guide — Basic to Extreme</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=Syne:wght@700;800&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({ startOnLoad: false, theme: 'dark', themeVariables: { primaryColor: '#eab308' } });</script>
<style>
:root {
--bg: #0f1117;
--card: #161b27;
--border: #2a3348;
--accent: #eab308;
--text-main: #ffffff;
--text-dim: #e2e8f0;
--code-bg: #0a0c10;
--basic: #10b981;
--inter: #3b82f6;
--adv: #8b5cf6;
--extreme: #ef4444;
--success: #34d399;
}
*{margin:0;padding:0;box-sizing:border-box}
body{background:var(--bg);color:var(--text-main);font-family:'Inter',sans-serif;overflow-x:hidden}
.topbar{background:var(--card);border-bottom:1px solid var(--border);padding:.75rem 1.25rem;display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;z-index:1000}
.logo{font-family:'Syne',sans-serif;font-size:1rem;font-weight:800;color:var(--accent);display:flex;align-items:center;gap:.75rem}
.logo span{color:var(--text-dim);font-weight:400;font-size:.8rem}
.back-hub{color:var(--text-dim);text-decoration:none;font-size:.8rem;padding:6px 12px;border:1px solid var(--border);border-radius:8px;transition:all .2s}
.back-hub:hover{background:var(--border);color:var(--accent)}
.layout{display:flex;height:calc(100vh - 56px)}
.sidebar{width:350px;background:var(--card);border-right:1px solid var(--border);display:flex;flex-direction:column;transition:all .3s}
.main{flex:1;overflow-y:auto;padding:2rem;background:radial-gradient(circle at top right, rgba(234,179,8,0.03), transparent)}
.sb-header{padding:1.25rem;border-bottom:1px solid var(--border)}
.search{width:100%;background:var(--bg);border:1px solid var(--border);border-radius:8px;padding:.6rem .8rem;color:#fff;font-family:inherit;font-size:.85rem;outline:none}
.search:focus{border-color:var(--accent)}
.q-list{flex:1;overflow-y:auto;padding:1rem}
.cat-group{margin-bottom:1.5rem}
.cat-title{font-size:.65rem;font-weight:800;color:var(--accent);text-transform:uppercase;letter-spacing:0.1em;margin-bottom:.75rem;padding-left:.5rem}
.q-item{padding:.85rem;border-radius:10px;border:1px solid transparent;cursor:pointer;margin-bottom:.4rem;transition:all .2s}
.q-item:hover{background:rgba(255,255,255,0.03);border-color:var(--border)}
.q-item.active{background:rgba(234,179,8,0.1);border-color:var(--accent)}
.q-title{font-size:.82rem;font-weight:500;margin-bottom:.4rem;line-height:1.4}
.q-meta{display:flex;gap:.5rem;align-items:center}
.level-dot{width:6px;height:6px;border-radius:50%}
.ans-card{max-width:900px;margin:0 auto}
.ans-header{margin-bottom:2rem}
.ans-level{display:inline-block;padding:4px 10px;border-radius:6px;font-size:.65rem;font-weight:700;text-transform:uppercase;letter-spacing:0.05em;margin-bottom:1rem}
.level-basic{background:rgba(16,185,129,0.15);color:var(--basic);border:1px solid rgba(16,185,129,0.3)}
.level-intermediate{background:rgba(59,130,246,0.15);color:var(--inter);border:1px solid rgba(59,130,246,0.3)}
.level-advanced{background:rgba(139,92,246,0.15);color:var(--adv);border:1px solid rgba(139,92,246,0.3)}
.level-extreme{background:rgba(239,68,68,0.15);color:var(--extreme);border:1px solid rgba(239,68,68,0.3)}
.ans-title{font-family:'Syne',sans-serif;font-size:2.2rem;font-weight:800;letter-spacing:-0.02em;line-height:1.1;margin-bottom:1.5rem}
.ans-def{font-size:1.1rem;color:var(--text-dim);line-height:1.6;margin-bottom:2rem}
.sec-title{font-family:'Syne',sans-serif;font-size:.9rem;font-weight:700;text-transform:uppercase;letter-spacing:0.1em;color:var(--accent);margin-bottom:1.5rem;display:flex;align-items:center;gap:.5rem;margin-top:3rem}
.sec-title::after{content:'';flex:1;height:1px;background:var(--border)}
.expl-list{list-style:none;margin-bottom:2.5rem}
.expl-item{position:relative;padding-left:1.5rem;margin-bottom:1rem;font-size:.95rem;color:var(--text-dim);line-height:1.6}
.expl-item::before{content:'→';position:absolute;left:0;color:var(--accent);font-weight:700}
.visual-wrap{background:var(--card);border:1px solid var(--border);border-radius:12px;padding:2rem;margin-bottom:2rem;display:flex;justify-content:center;overflow:hidden}
.mermaid{background:transparent !important}
.code-wrap{background:var(--code-bg);border:1px solid var(--border);border-radius:12px;margin-bottom:1.5rem;overflow:hidden}
.code-header{padding:.75rem 1rem;background:rgba(255,255,255,0.03);border-bottom:1px solid var(--border);display:flex;justify-content:space-between;align-items:center}
.code-lang{font-size:.7rem;font-family:'JetBrains Mono',monospace;color:var(--text-dim);text-transform:uppercase}
.code-body{padding:1.25rem;font-family:'JetBrains Mono',monospace;font-size:.88rem;line-height:1.6;overflow-x:auto;color:#d1d5db}
.kw{color:#fbbf24} .fn{color:#60a5fa} .str{color:#34d399} .num{color:#f87171} .com{color:#6b7280} .op{color:#818cf8}
.output-wrap{background:#000;border:1px solid var(--border);border-radius:12px;padding:1rem;font-family:'JetBrains Mono',monospace;margin-bottom:2.5rem}
.output-title{font-size:.65rem;color:var(--text-dim);margin-bottom:.5rem;text-transform:uppercase;letter-spacing:0.1em}
.output-text{color:var(--success);font-size:.85rem;white-space:pre-wrap}
.tip-box{background:linear-gradient(135deg, rgba(234,179,8,0.1) 0%, transparent 100%);border:1px solid rgba(234,179,8,0.2);border-radius:12px;padding:1.5rem;position:relative;overflow:hidden;margin-top:2rem}
.tip-box::before{content:'PRO TIP';position:absolute;top:0;right:0;background:var(--accent);color:#000;font-size:.6rem;font-weight:800;padding:4px 10px;border-bottom-left-radius:8px}
.tip-text{font-size:.92rem;color:var(--text-dim);line-height:1.6}
.mob-menu-btn{display:none}
@media(max-width:900px){
.sidebar{position:fixed;left:-350px;top:56px;height:calc(100vh - 56px);z-index:900}
.sidebar.open{left:0}
.mob-menu-btn{display:block;background:none;border:none;color:var(--accent);font-size:1.5rem;cursor:pointer}
}
.welcome{text-align:center;padding-top:10vh}
.welcome h1{font-family:'Syne',sans-serif;font-size:3.5rem;margin-bottom:1rem;background:linear-gradient(to right, #fff, var(--accent));-webkit-background-clip:text;-webkit-text-fill-color:transparent}
.welcome p{color:var(--text-dim);max-width:500px;margin:0 auto 2rem;font-size:1.1rem}
</style>
</head>
<body>
<div class="topbar">
<div class="logo">
<button class="mob-menu-btn" onclick="toggleSidebar()">☰</button>
JS <span>Interview Master</span>
</div>
<a href="index.html" class="back-hub">← Mastery Hub</a>
</div>
<div class="layout">
<div class="sidebar" id="sidebar">
<div class="sb-header">
<input type="text" class="search" placeholder="Search questions..." onkeyup="filterQ()">
</div>
<div class="q-list" id="qList"></div>
</div>
<div class="main" id="mainContent">
<div class="welcome" id="welcomeScreen">
<h1>JS: Basic to Extreme</h1>
<p>A comprehensive deep dive into JavaScript internals, patterns, and senior-level challenges.</p>
<div style="color:var(--accent); font-size: 2rem;">⚡</div>
</div>
<div class="ans-card" id="ansCard" style="display:none"></div>
</div>
</div>
<script>
const questions = [
// ── 1. JAVASCRIPT BASICS ──
{
id: 1,
title: "What is JavaScript?",
level: "basic",
category: "1. Basics",
def: "<strong>JavaScript</strong> is a high-level, interpreted (JIT), multi-paradigm language that serves as the engine of the modern web.",
expl: [
"Dynamic & Weakly Typed: Variables are not bound to a specific type.",
"Single-threaded: Executes one task at a time on the main thread.",
"Non-blocking: Uses the Event Loop to handle asynchronous operations.",
"Prototype-based: Objects inherit directly from other objects."
],
code: `const msg = "JS is everywhere";\nconsole.log(typeof msg);`,
output: "\"string\"",
tip: "Emphasize its 'Single-threaded but non-blocking' nature as a core strength."
},
{
id: 2,
title: "Difference between JavaScript and TypeScript?",
level: "basic",
category: "1. Basics",
def: "TypeScript is a strongly typed superset of JavaScript developed by Microsoft.",
expl: [
"TS provides static typing; JS is dynamic.",
"TS catches type-related bugs at compile time.",
"TS requires a transpilation step (TSC) to run in browsers.",
"TS supports features like Interfaces and Generics more robustly."
],
code: `// TS\nlet id: number = 10;\n// JS\nlet id = 10;`,
output: "// Compiled JS is identical",
tip: "Mention that TS is great for large-scale enterprise applications to reduce runtime errors."
},
{
id: 3,
title: "Difference between var, let, and const?",
level: "basic",
category: "1. Basics",
def: "Declaration keywords with different scoping and hoisting rules.",
expl: [
"var: Function-scoped, hoisted (as undefined), can be redeclared.",
"let: Block-scoped, hoisted to TDZ, cannot be redeclared in same scope.",
"const: Block-scoped, immutable binding (reference cannot change), must be initialized."
],
mermaid: "graph TD\n V[var] --> FS[Function Scope]\n L[let] --> BS[Block Scope]\n C[const] --> BS\n BS --> TDZ[Temporal Dead Zone]",
code: `{\n var x = 1;\n let y = 2;\n}\nconsole.log(x); // 1\nconsole.log(y); // Error`,
output: "1\nReferenceError: y is not defined",
tip: "Explain the 'Temporal Dead Zone' for let/const to show deeper knowledge."
},
{
id: 4,
title: "What are primitive data types?",
level: "basic",
category: "1. Basics",
def: "Basic data types that are not objects and have no methods.",
expl: [
"String, Number, BigInt, Boolean, Undefined, Null, Symbol.",
"Primitives are passed by value and are immutable.",
"All other types (Array, Object, Function) are reference types."
],
code: `let a = 10;\nlet b = a;\nb = 20;\nconsole.log(a); // 10`,
output: "10",
tip: "Remember 'typeof null' is 'object'—it's a known JS bug."
},
{
id: 5,
title: "Difference between null and undefined?",
level: "basic",
category: "1. Basics",
def: "Both represent absence of value, but with different semantic meanings.",
expl: [
"undefined: Variable declared but not assigned a value (automatic).",
"null: Intentional absence of any object value (manual).",
"typeof undefined is 'undefined'; typeof null is 'object'."
],
code: `let x;\nlet y = null;\nconsole.log(x == y); // true\nconsole.log(x === y); // false`,
output: "true\nfalse",
tip: "Use null to explicitly 'clear' a variable or indicate a missing result."
},
{
id: 6,
title: "Difference between == and ===?",
level: "basic",
category: "1. Basics",
def: "Comparison operators with and without type coercion.",
expl: [
"== (Abstract Equality): Compares values after converting to a common type.",
"=== (Strict Equality): Compares both value and type without conversion.",
"Standard best practice is to always use ===."
],
code: `console.log(5 == '5'); // true\nconsole.log(5 === '5'); // false`,
output: "true\nfalse",
tip: "Mention that == is useful only in very specific cases like checking for both null/undefined (val == null)."
},
{
id: 7,
title: "What is type coercion?",
level: "basic",
category: "1. Basics",
def: "The automatic or implicit conversion of values from one data type to another.",
expl: [
"Implicit: Happens automatically (e.g., '5' + 5 = '55').",
"Explicit: Done manually using functions like Number() or String().",
"Tricky cases: [] + [] = '', [] + {} = '[object Object]'."
],
code: `console.log('5' - 2); // 3 (String becomes Number)\nconsole.log('5' + 2); // '52' (Number becomes String)`,
output: "3\n\"52\"",
tip: "Know the difference between '+' (prefers string) and other operators (prefer numbers)."
},
{
id: 8,
title: "Truthy and Falsy values",
level: "basic",
category: "1. Basics",
def: "Values that evaluate to true or false in a boolean context.",
expl: [
"Falsy: false, 0, -0, 0n, '', null, undefined, NaN.",
"Truthy: Everything else (including [], {}, 'false').",
"Common mistake: Empty arrays and objects are truthy."
],
code: `if ([]) console.log("Truthy!");\nif ("") console.log("Won't run");`,
output: "\"Truthy!\"",
tip: "Be careful with 0 and empty strings in conditional checks; use explicit comparison if needed."
},
{
id: 9,
title: "What is Hoisting?",
level: "basic",
category: "1. Basics",
def: "The behavior where declarations are moved to the top of their scope during compilation.",
expl: [
"Function declarations are fully hoisted.",
"Var is hoisted and initialized as undefined.",
"Let/Const are hoisted but remain in the Temporal Dead Zone (TDZ).",
"Initializations (assignments) are NOT hoisted."
],
code: `console.log(a); // undefined\nvar a = 5;\n\nhello(); // "Hi"\nfunction hello() { console.log("Hi"); }`,
output: "undefined\n\"Hi\"",
tip: "Always declare variables at the top of their scope to avoid hoisting confusion."
},
{
id: 10,
title: "Explain scope in JavaScript",
level: "basic",
category: "1. Basics",
def: "The accessibility of variables and functions in different parts of code.",
expl: [
"Global Scope: Accessible everywhere.",
"Function Scope: Accessible only within the function (var).",
"Block Scope: Accessible only within the {} block (let/const).",
"Lexical Scope: Outer variables accessible to inner functions."
],
mermaid: "graph TD\n GS[Global Scope] --> FS[Function Scope]\n FS --> BS[Block Scope]",
code: `const global = "I am global";\nfunction test() {\n const local = "I am local";\n console.log(global);\n}\ntest();`,
output: "\"I am global\"",
tip: "Minimize global variables to prevent naming collisions and memory bloat."
},
{
id: 11,
title: "What is block scope?",
level: "basic",
category: "1. Basics",
def: "Scope restricted to the nearest curly braces {}.",
expl: [
"Introduced with ES6 (let/const).",
"Applies to if-statements, for-loops, and naked blocks.",
"Helps prevent variable leakage from loops and conditions."
],
code: `if (true) {\n let x = 10;\n}\nconsole.log(x); // ReferenceError`,
output: "Uncaught ReferenceError: x is not defined",
tip: "Block scope makes code more predictable and debugging easier."
},
{
id: 12,
title: "Function Declaration vs Expression?",
level: "basic",
category: "1. Basics",
def: "Two ways to define functions with different hoisting behavior.",
expl: [
"Declaration: <code>function name() {}</code>. Hoisted fully.",
"Expression: <code>const name = function() {}</code>. Not hoisted (TDZ).",
"Expressions allow for anonymous functions and immediate use in HOFs."
],
code: `sayHi(); // Works\nfunction sayHi() { console.log('Hi'); }\n\nsayBye(); // Error\nconst sayBye = () => console.log('Bye');`,
output: "\"Hi\"\nUncaught ReferenceError: sayBye is not defined",
tip: "Use expressions (const + arrow) to enforce clean code order (declaration before use)."
},
{
id: 13,
title: "What are Arrow Functions?",
level: "basic",
category: "1. Basics",
def: "A shorter syntax for writing functions introduced in ES6.",
expl: [
"No <code>this</code>, <code>arguments</code>, or <code>super</code> bindings.",
"Cannot be used as constructors (new).",
"Implicit return for single-line expressions."
],
code: `const add = (a, b) => a + b;\nconsole.log(add(2, 3));`,
output: "5",
tip: "Arrow functions are perfect for callbacks and functional methods (map/filter)."
},
{
id: 14,
title: "Arrow vs Regular Functions",
level: "basic",
category: "1. Basics",
def: "Key differences in behavior, especially regarding the 'this' keyword.",
expl: [
"Regular: <code>this</code> is dynamic (depends on how it's called).",
"Arrow: <code>this</code> is lexical (inherited from parent scope).",
"Regular functions have an <code>arguments</code> object; arrows do not.",
"Arrows cannot be constructors; regulars can."
],
code: `const obj = {\n val: 10,\n reg: function() { console.log(this.val); },\n arr: () => console.log(this.val)\n};\nobj.reg(); // 10\nobj.arr(); // undefined`,
output: "10\nundefined",
tip: "Use regular functions for methods on objects, and arrows for internal callbacks."
},
{
id: 15,
title: "What is 'this' in JavaScript?",
level: "basic",
category: "1. Basics",
def: "A keyword referring to the context in which a function is executing.",
expl: [
"Global Context: Window (in browsers).",
"Method Call: The object before the dot.",
"Constructor Call: The newly created instance.",
"Arrow Function: Inherited from the surrounding scope."
],
code: `function checkThis() { console.log(this); }\ncheckThis(); // Window\n\nconst obj = { m: checkThis };\nobj.m(); // obj`,
output: "Window\n{ m: f }",
tip: "Master call(), apply(), and bind() to manually control 'this' context."
},
{
id: 16,
title: "Explain Template Literals",
level: "basic",
category: "1. Basics",
def: "Modern string syntax using backticks `` allowing for interpolation and multi-line strings.",
expl: [
"Use <code>${expression}</code> to inject variables.",
"Supports multi-line strings without escape characters.",
"Can be used for 'Tagged Templates' (advanced)."
],
code: `const name = 'JS';\nconsole.log(\`Hello, \${name}!\\nNext line.\`);`,
output: "\"Hello, JS!\\nNext line.\"",
tip: "Template literals are much cleaner than string concatenation with '+'."
},
{
id: 17,
title: "Map, Filter, and Reduce",
level: "basic",
category: "1. Basics",
def: "Higher-order array methods for functional data processing.",
expl: [
"Map: Returns a new array with transformed elements.",
"Filter: Returns a new array with elements that pass a test.",
"Reduce: Reduces array to a single value (sum, object, etc.).",
"None of these mutate the original array."
],
code: `const nums = [1, 2, 3];\nconst doubled = nums.map(n => n * 2);\nconst evens = nums.filter(n => n % 2 === 0);\nconst sum = nums.reduce((acc, n) => acc + n, 0);`,
output: "// doubled: [2,4,6], evens: [2], sum: 6",
tip: "Map is for 1:1 transformations; Filter is for subsets; Reduce is for anything else."
},
{
id: 18,
title: "forEach vs map",
level: "basic",
category: "1. Basics",
def: "Two ways to iterate over arrays with different return values.",
expl: [
"forEach: Just executes a callback; returns undefined.",
"map: Executes a callback and returns a NEW array.",
"Use forEach for side effects (logging); use map for data transformations."
],
code: `const arr = [1, 2];\nconst a = arr.forEach(x => x * 2);\nconst b = arr.map(x => x * 2);`,
output: "// a: undefined, b: [2, 4]",
tip: "Never use map if you don't intend to use the returned array."
},
{
id: 19,
title: "What are callbacks?",
level: "basic",
category: "1. Basics",
def: "Functions passed as arguments to other functions to be executed later.",
expl: [
"Used extensively in asynchronous operations (timers, events).",
"Allows for asynchronous execution in a single-threaded language.",
"Can lead to 'Callback Hell' if nested too deeply."
],
code: `function greet(name, callback) {\n console.log('Hello ' + name);\n callback();\n}\ngreet('User', () => console.log('Done'));`,
output: "\"Hello User\"\n\"Done\"",
tip: "Callbacks are the foundation, but modern JS prefers Promises and Async/Await."
},
{
id: 20,
title: "Higher-Order Functions",
level: "basic",
category: "1. Basics",
def: "Functions that take other functions as arguments or return them.",
expl: [
"Map, Filter, Reduce are HOFs.",
"Essential for functional programming patterns.",
"Allows for code abstraction and reusability."
],
code: `const multiplier = (factor) => (num) => num * factor;\nconst double = multiplier(2);\nconsole.log(double(5));`,
output: "10",
tip: "Explain that HOFs treat functions as 'first-class citizens'."
},
{
id: 21,
title: "What are Closures?",
level: "basic",
category: "1. Basics",
def: "A function bundled together with references to its surrounding state (lexical environment).",
expl: [
"Inner functions have access to variables defined in their outer scope.",
"This scope persists even after the outer function finishes execution.",
"Used for data privacy (encapsulation) and function factories."
],
mermaid: "graph LR\n Outer[Outer scope] -- Persists --> Inner[Inner Function]\n Inner -- Accesses --> OuterVar[Outer Variables]",
code: `function outer() {\n let secret = 'shh';\n return () => console.log(secret);\n}\nconst tell = outer();\ntell();`,
output: "\"shh\"",
tip: "Closures are the key to modular, private code in JavaScript."
},
{
id: 22,
title: "Explain Lexical Scope",
level: "basic",
category: "1. Basics",
def: "The scope of a variable is determined by its position in the source code.",
expl: [
"Inner functions can access variables in their parent's scope.",
"The engine looks 'upwards' through the scope chain.",
"It is 'static', meaning it doesn't change during runtime."
],
code: `const x = 10;\nfunction a() {\n console.log(x); // Accesses global x\n}\na();`,
output: "10",
tip: "Contrast this with 'Dynamic Scope' (which JS does not have) to impress interviewers."
},
{
id: 23,
title: "Spread Operator (...)",
level: "basic",
category: "1. Basics",
def: "Allows an iterable (array/object) to be expanded in places where zero or more arguments or elements are expected.",
expl: [
"Arrays: <code>const combined = [...a, ...b];</code>.",
"Objects: <code>const newObj = {...oldObj};</code> (Shallow Copy).",
"Function Calls: <code>Math.max(...nums);</code>."
],
code: `const arr = [1, 2];\nconst copy = [...arr, 3];\nconsole.log(copy);`,
output: "[1, 2, 3]",
tip: "Mention it only performs a shallow copy for nested elements."
},
{
id: 24,
title: "What is Destructuring?",
level: "basic",
category: "1. Basics",
def: "A syntax to extract values from arrays or properties from objects into distinct variables.",
expl: [
"Objects: <code>const { name, age } = user;</code>.",
"Arrays: <code>const [first, second] = list;</code>.",
"Can provide default values and aliases."
],
code: `const obj = { x: 1, y: 2 };\nconst { x: xCoord } = obj;\nconsole.log(xCoord);`,
output: "1",
tip: "Use destructuring in function parameters for cleaner, more readable code."
},
{
id: 25,
title: "Default Parameters",
level: "basic",
category: "1. Basics",
def: "Allows formal parameters to be initialized with default values if no value or undefined is passed.",
expl: [
"Syntax: <code>function (a = 10) {}</code>.",
"Default values are evaluated at call time.",
"Only triggered by <code>undefined</code>, not <code>null</code> or <code>0</code>."
],
code: `function greet(name = 'Guest') { console.log(name); }\ngreet();\ngreet(undefined);\ngreet(null);`,
output: "\"Guest\"\n\"Guest\"\nnull",
tip: "Remember that null is considered a 'value' and won't trigger the default."
},
{
id: 26,
title: "Optional Chaining (?.)",
level: "basic",
category: "1. Basics",
def: "Allows reading the value of a property located deep within a chain of connected objects without checking each reference.",
expl: [
"Stops and returns <code>undefined</code> if a reference is nullish.",
"Prevents 'Cannot read property X of undefined' errors.",
"Works for function calls too: <code>obj.method?.()</code>."
],
code: `const user = {};\nconsole.log(user?.address?.city);`,
output: "undefined",
tip: "Essential for handling API responses where some fields might be missing."
},
{
id: 27,
title: "Nullish Coalescing (??)",
level: "basic",
category: "1. Basics",
def: "A logical operator that returns its right-hand side operand when its left-hand side is null or undefined.",
expl: [
"Difference from <code>||</code>: <code>||</code> fails for 0 and ''.",
"<code>??</code> only fails for <code>null</code> and <code>undefined</code>.",
"Great for setting default config values."
],
code: `const count = 0;\nconsole.log(count || 10); // 10\nconsole.log(count ?? 10); // 0`,
output: "10\n0",
tip: "Use ?? when you want to treat 0 or '' as valid values."
},
{
id: 28,
title: "Shallow vs Deep Copy",
level: "basic",
category: "1. Basics",
def: "Ways to copy objects with varying levels of independence for nested properties.",
expl: [
"Shallow: Only the top-level properties are copied. Nested objects share references.",
"Deep: All levels are copied recursively. Changes never affect the original.",
"Methods: <code>{...obj}</code> (Shallow), <code>structuredClone(obj)</code> (Deep)."
],
code: `const original = { a: { b: 1 } };\nconst shallow = { ...original };\nshallow.a.b = 2;\nconsole.log(original.a.b); // 2`,
output: "2",
tip: "Avoid JSON.parse(JSON.stringify()) for deep copies as it loses methods and Symbols."
},
{
id: 29,
title: "What is JSON?",
level: "basic",
category: "1. Basics",
def: "JavaScript Object Notation — a lightweight format for storing and transporting data.",
expl: [
"Language-independent data format.",
"JSON.stringify(): Object -> String.",
"JSON.parse(): String -> Object.",
"Requires double quotes for keys and strings."
],
code: `const str = '{"id": 1}';\nconst obj = JSON.parse(str);\nconsole.log(obj.id);`,
output: "1",
tip: "Remember that JSON cannot store functions or circular references."
},
{
id: 30,
title: "parseInt vs Number()",
level: "basic",
category: "1. Basics",
def: "Two ways to convert strings to numbers with different parsing logic.",
expl: [
"parseInt: Parses until it hits a non-digit character. Can specify radix (base).",
"Number(): Strict conversion. Returns NaN if the string contains non-numeric characters.",
"parseInt('10px') = 10; Number('10px') = NaN."
],
code: `console.log(parseInt('10.5'));\nconsole.log(Number('10.5'));`,
output: "10\n10.5",
tip: "Always specify the radix (usually 10) in parseInt() to avoid legacy octal issues."
},
// ── 2. ARRAYS & OBJECTS ──
{
id: 31,
title: "How do arrays work internally?",
level: "advanced",
category: "2. Arrays & Objects",
def: "JS Arrays are specialized objects that V8 optimizes for performance using different internal modes.",
expl: [
"SMI (Small Integer): Packed contiguous memory for integers.",
"Double: For floating point numbers.",
"Holey: When indices are missing (sparse).",
"Dictionary Mode: Slower hash-map based storage for very sparse or non-indexed properties."
],
mermaid: "graph LR\n P[Packed SMI] --> PD[Packed Double]\n PD --> PH[Packed Holey]\n PH --> D[Dictionary Elements]",
code: `const arr = [1, 2, 3]; // SMI\narr.push(4.5); // Transition to Double\narr[100] = 10; // Transition to Holey`,
output: "// V8 internal state transition",
tip: "Maintain 'Packed' arrays by avoiding holes and consistent types for max speed."
},
{
id: 32,
title: "Mutable vs Immutable Operations",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Difference between changing an existing object versus creating a new one.",
expl: [
"Mutable: push(), pop(), splice(), sort(), reverse().",
"Immutable: concat(), slice(), map(), filter(), spread (...).",
"Immutability is preferred in state management (Redux/React)."
],
code: `const a = [1];\na.push(2); // Mutated\nconst b = [...a, 3]; // Immutable`,
output: "// a: [1, 2], b: [1, 2, 3]",
tip: "Always use immutable operations when dealing with React state to ensure proper re-rendering."
},
{
id: 33,
title: "Remove duplicates from an array",
level: "basic",
category: "2. Arrays & Objects",
def: "Techniques to filter unique elements.",
expl: [
"Set: <code>[...new Set(arr)]</code> (Fastest/Cleanest).",
"Filter: <code>arr.filter((val, i) => arr.indexOf(val) === i)</code>.",
"Reduce: Building an accumulator object/map."
],
code: `const arr = [1, 1, 2, 2, 3];\nconst unique = [...new Set(arr)];`,
output: "[1, 2, 3]",
tip: "Set is O(n) while filter + indexOf is O(n²) Alexandr prefer Set for performance."
},
{
id: 34,
title: "How to flatten nested arrays?",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Reducing multi-dimensional arrays into a single dimension.",
expl: [
"flat(): <code>arr.flat(depth)</code> (Modern).",
"flatMap(): Map and then flatten by 1 level.",
"Recursion: For very old environments or custom logic."
],
code: `const nested = [1, [2, [3]]];\nconsole.log(nested.flat(2));`,
output: "[1, 2, 3]",
tip: "Use Infinity as depth to flatten an array of any depth: arr.flat(Infinity)."
},
{
id: 35,
title: "What is Object Destructuring?",
level: "basic",
category: "2. Arrays & Objects",
def: "Extracting multiple properties from an object into variables in a single statement.",
expl: [
"Saves lines of code.",
"Allows for default values: <code>const { x = 0 } = obj;</code>.",
"Supports renaming: <code>const { x: xPos } = obj;</code>."
],
code: `const user = { name: 'Ali', role: 'Dev' };\nconst { name, role } = user;`,
output: "// name: 'Ali', role: 'Dev'",
tip: "Use it to pluck exactly what you need from large API response objects."
},
{
id: 36,
title: "Object.freeze() vs Object.seal()",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Methods to restrict modifications to an object.",
expl: [
"Seal: Cannot add/delete properties, but CAN modify existing ones.",
"Freeze: Cannot add, delete, OR modify existing properties. (Read-only).",
"Both are shallow—they don't protect nested objects automatically."
],
code: `const obj = { a: 1 };\nObject.freeze(obj);\nobj.a = 2; // Fails silently (or error in strict mode)`,
output: "// obj.a remains 1",
tip: "Freeze is useful for constants and configuration objects that must never change."
},
{
id: 37,
title: "What is Object.create()?",
level: "advanced",
category: "2. Arrays & Objects",
def: "Creates a new object using an existing object as the prototype.",
expl: [
"Allows for explicit prototype linking.",
"Can create an object with NO prototype (Object.create(null)).",
"Useful for inheritance patterns without classes."
],
code: `const proto = { greet: () => 'Hi' };\nconst obj = Object.create(proto);\nconsole.log(obj.greet());`,
output: "\"Hi\"",
tip: "Use Object.create(null) for 'Dictionary' objects to avoid prototype pollution risks."
},
{
id: 38,
title: "in operator vs hasOwnProperty()",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Different ways to check if a property exists on an object.",
expl: [
"in: Checks the object AND its prototype chain.",
"hasOwnProperty: Checks ONLY the object itself (not inherited).",
"Modern alternative: Object.hasOwn(obj, prop)."
],
code: `const obj = { a: 1 };\nconsole.log('a' in obj);\nconsole.log('toString' in obj);\nconsole.log(obj.hasOwnProperty('toString'));`,
output: "true\ntrue\nfalse",
tip: "Always use hasOwnProperty or Object.hasOwn when iterating over properties to avoid inherited junk."
},
{
id: 39,
title: "How does Object Reference work?",
level: "basic",
category: "2. Arrays & Objects",
def: "Objects are stored in memory and variables hold a pointer (reference) to that memory address.",
expl: [
"Copying an object variable only copies the reference.",
"Modifying through one variable affects all references.",
"Comparison (==) checks if both point to the SAME memory address."
],
code: `const a = { x: 1 };\nconst b = a;\nb.x = 2;\nconsole.log(a.x); // 2`,
output: "2",
tip: "This is why you need 'Shallow' or 'Deep' copies to create independent objects."
},
{
id: 40,
title: "Pass by Value vs Pass by Reference",
level: "intermediate",
category: "2. Arrays & Objects",
def: "How data is passed into functions.",
expl: [
"Primitives (Value): Function gets a copy. Changing it inside doesn't affect outside.",
"Objects (Reference): Function gets a reference. Mutating properties affects outside.",
"Wait: Reassigning the whole object inside doesn't affect outside reference."
],
code: `function mut(o) { o.x = 2; }\nconst obj = { x: 1 };\nmut(obj);\nconsole.log(obj.x);`,
output: "2",
tip: "Technically, JS is 'Call by Sharing'. Values are copies, but for objects, the value IS the reference."
},
{
id: 41,
title: "Maps and Sets",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Modern collection types introduced in ES6.",
expl: [
"Map: Key-value pairs where keys can be ANY type (objects, functions).",
"Set: Collection of UNIQUE values.",
"Both preserve insertion order and are iterable."
],
code: `const m = new Map(); m.set({}, 'Val');\nconst s = new Set([1, 1, 2]);`,
output: "// m.size is 1, s.size is 2",
tip: "Use Map for heavy additions/removals and non-string keys."
},
{
id: 42,
title: "Object vs Map",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Comparison of the two key-value storage methods.",
expl: [
"Keys: Object keys are strings/symbols; Map keys can be anything.",
"Size: Map has a .size property; Objects require manual counting.",
"Performance: Map is optimized for frequent additions/removals.",
"Prototypes: Objects have default keys (toString, etc.); Maps are clean."
],
code: `const map = new Map();\nmap.set(123, 'Number key');\nconst obj = { 123: 'Stringified key' };`,
output: "// map[123] vs obj['123']",
tip: "Default to Object for simple data; use Map for complex logic or large datasets."
},
{
id: 43,
title: "How to clone an object deeply?",
level: "intermediate",
category: "2. Arrays & Objects",
def: "Creating a completely independent copy of a nested structure.",
expl: [
"Modern: structuredClone(obj).",
"Legacy/Safe: JSON.parse(JSON.stringify(obj)) - limited.",
"Library: _.cloneDeep (lodash).",
"Manual: Recursive function (complex edge cases)."
],
code: `const original = { a: { b: 1 } };\nconst deep = structuredClone(original);\ndeep.a.b = 99;\nconsole.log(original.a.b); // 1`,
output: "1",
tip: "structuredClone() is built into the platform now. Use it!"
},
// ── 3. FUNCTIONS ──
{
id: 44,
title: "What is Currying?",
level: "intermediate",
category: "3. Functions",
def: "Technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument.",
expl: [
"Allows for partial application of arguments.",
"Helps in creating reusable utility functions.",
"Foundation for functional composition."
],
code: `const sum = a => b => a + b;\nconst addFive = sum(5);\nconsole.log(addFive(10));`,
output: "15",
tip: "Currying makes code more modular and declarative."
},
{
id: 45,
title: "What is Function Composition?",
level: "advanced",
category: "3. Functions",
def: "Combining two or more functions to produce a new function.",
expl: [
"Math: f(g(x)).",
"Pipes: Data flows through a series of transformations.",
"Reduces intermediate variable noise."
],
code: `const compose = (f, g) => (x) => f(g(x));\nconst add1 = x => x + 1;\nconst double = x => x * 2;\nconst addThenDouble = compose(double, add1);\nconsole.log(addThenDouble(5));`,
output: "12",
tip: "composition is better than nesting functions deeply."
},
{
id: 46,
title: "Explain IIFE",
level: "intermediate",
category: "3. Functions",
def: "Immediately Invoked Function Expression.",
expl: [
"Executed as soon as it is defined.",
"Creates a private scope, preventing global namespace pollution.",
"Used before ES6 modules to encapsulate code."
],
code: `(function() {\n const secret = 'IIFE';\n console.log('Running!');\n})();`,
output: "\"Running!\"",
tip: "Mostly replaced by ESM, but still useful for one-off async initializations."
},
{
id: 47,
title: "What is Memoization?",
level: "advanced",
category: "3. Functions",
def: "An optimization technique that caches the results of expensive function calls.",
expl: [
"Checks if the result for given arguments is already in the cache.",
"Returns cached result if hit; computes and caches if miss.",
"Requires pure functions (same input = same output)."
],
code: `const cache = {};\nfunction slow(n) {\n if (n in cache) return cache[n];\n const res = n * 2; // Expensive work\n cache[n] = res;\n return res;\n}`,
output: "// Second call is O(1)",
tip: "Use memoization for heavy UI calculations or recursive algorithms like Fibonacci."
},
{
id: 48,
title: "Debounce vs Throttle",
level: "advanced",
category: "3. Functions",
def: "Rate-limiting techniques for performance-heavy callbacks (resize, scroll, search).",
expl: [
"Debounce: Wait until the user STOPS for X ms before firing.",
"Throttle: Fire at most once every X ms, regardless of activity.",
"Analogy: Debounce is 'Elevator wait'; Throttle is 'Staggered traffic light'."
],
mermaid: "graph LR\n E[Events] --> D[Debounce]\n D --> |Last Only| F[Fire]\n E --> T[Throttle]\n T --> |Every X ms| F",
code: `// High frequency events\nwindow.addEventListener('resize', debounce(cb, 200));\nwindow.addEventListener('scroll', throttle(cb, 100));`,
output: "// Significant CPU usage reduction",
tip: "Use Debounce for search inputs and Throttle for scroll tracking."
},
{
id: 49,
title: "Implement Debounce Manually",
level: "advanced",
category: "3. Functions",
def: "Coding a debounce utility from scratch.",
expl: [
"Uses a closure to track the timer.",
"Clears previous timer on every call.",
"Starts a new timer that fires the callback."
],
code: `function debounce(fn, delay) {\n let timer;\n return (...args) => {\n clearTimeout(timer);\n timer = setTimeout(() => fn(...args), delay);\n };\n}`,
output: "// Closure holds 'timer' state",
tip: "Be prepared to write this on a whiteboard; it's a very common live coding task."
},
{
id: 50,
title: "Implement Throttle Manually",
level: "advanced",
category: "3. Functions",
def: "Coding a throttle utility from scratch.",
expl: [
"Uses a flag (boolean) or timestamp to track 'cool down'.",
"Only fires if enough time has passed.",
"Ignores calls made during the cool down period."
],
code: `function throttle(fn, limit) {\n let wait = false;\n return (...args) => {\n if (wait) return;\n fn(...args);\n wait = true;\n setTimeout(() => wait = false, limit);\n };\n}`,
output: "// Closure holds 'wait' state",
tip: "Explain that throttle ensures the UI stays responsive without overloading the main thread."
},
{
id: 51,
title: "Sync vs Async Functions",
level: "basic",
category: "3. Functions",
def: "The execution flow of code relative to time.",
expl: [
"Synchronous: Line-by-line. Next line waits for the current one to finish.",
"Asynchronous: Task starts now, finishes later. Doesn't block the rest of the code.",
"JS uses the Event Loop to make async possible on a single thread."
],
code: `console.log(1);\nsetTimeout(() => console.log(2), 0);\nconsole.log(3);`,
output: "1\n3\n2",
tip: "Async is non-blocking, which is why the browser stays responsive during API calls."
},
{
id: 52,
title: "What are Pure Functions?",
level: "intermediate",
category: "3. Functions",
def: "Functions that produce the same output for the same input and have no side effects.",
expl: [
"Predictable and easy to test.",
"Doesn't modify variables outside its scope.",
"Fundamental for React components and Redux reducers."
],
code: `const add = (a, b) => a + b; // Pure\n\nlet total = 0;\nconst addT = (n) => total += n; // Impure (Side effect)`,
output: "// add(2,2) is always 4",
tip: "Purity enables 'Referential Transparency', where a function call can be replaced by its result."
},
{
id: 53,
title: "What are Side Effects?",
level: "intermediate",
category: "3. Functions",
def: "Any interaction with the outside world from within a function.",
expl: [
"Modifying global variables.",
"Console logging or UI updates.",
"API calls (Network I/O).",
"Reading/Writing to the DOM."
],
code: `function updateUI() {\n document.body.innerText = 'Changed';\n}`,
output: "// State outside function changed",
tip: "In React, use useEffect to isolate side effects from the pure rendering cycle."
},
// ── 4. ASYNCHRONOUS JAVASCRIPT ──
{
id: 54,
title: "What is the Event Loop?",
level: "advanced",
category: "4. Async",
def: "The mechanism that allows JS to perform non-blocking I/O operations despite being single-threaded.",
expl: [
"Monitors the Call Stack and the Callback Queue.",
"If the stack is empty, it pushes the first task from the queue to the stack.",
"Consists of Macrotask and Microtask queues."
],
mermaid: "graph TD\n S[Call Stack] --> W[Web APIs]\n W --> Q[Task Queue]\n Q --> |Event Loop| S",
code: `console.log('Start');\nsetTimeout(() => console.log('Timeout'), 0);\nconsole.log('End');`,
output: "\"Start\"\n\"End\"\n\"Timeout\"",
tip: "Explain that the Event Loop is part of the browser environment (Libuv in Node), not the JS engine (V8) itself."
},
{
id: 55,
title: "Explain Call Stack",
level: "basic",
category: "4. Async",
def: "A LIFO (Last-In, First-Out) structure that keeps track of function calls.",
expl: [
"When a function is called, it's pushed to the stack.",
"When it returns, it's popped off.",
"Synchronous code runs entirely on the stack."
],
code: `function a() { b(); }\nfunction b() { console.log('in b'); }\na();`,
output: "\"in b\"",
tip: "A 'Stack Overflow' occurs when you have too many recursive calls without a base case."
},
{
id: 56,
title: "What are Web APIs?",
level: "intermediate",
category: "4. Async",
def: "External functionalities provided by the browser environment.",
expl: [
"Examples: setTimeout, Fetch, DOM Events, Geolocation.",
"They run outside the JS engine and notify the engine via the Task Queue.",
"This is how 'multi-tasking' happens in a single-threaded language."
],
code: `fetch('https://api.com').then(res => console.log(res));`,
output: "// Request handled by browser network thread",
tip: "Distinguish between JS core features (V8) and Browser environment features (Web APIs)."
},
{
id: 57,
title: "Macro task vs Micro task?",
level: "advanced",
category: "4. Async",
def: "Two different queues for asynchronous callbacks with different priorities.",