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
727import { describe, it, expect } from 'vitest'
describe('Hoisting', () => {
describe('Variable Hoisting with var', () => {
it('should hoist var declarations and initialize to undefined', () => {
function example() {
const before = greeting
var greeting = "Hello"
const after = greeting
return { before, after }
}
const result = example()
expect(result.before).toBe(undefined)
expect(result.after).toBe("Hello")
})
it('should hoist var to function scope, not block scope', () => {
function example() {
if (true) {
var message = "Inside block"
}
return message
}
expect(example()).toBe("Inside block")
})
it('should hoist multiple var declarations', () => {
function example() {
const first = x
var x = 1
const second = x
var x = 2
const third = x
return { first, second, third }
}
const result = example()
expect(result.first).toBe(undefined)
expect(result.second).toBe(1)
expect(result.third).toBe(2)
})
it('should allow var redeclaration without error', () => {
var name = "Alice"
var name = "Bob"
var name = "Charlie"
expect(name).toBe("Charlie")
})
})
describe('let and const: Temporal Dead Zone', () => {
it('should throw ReferenceError when accessing let before declaration', () => {
expect(() => {
eval(`
const before = x
let x = 10
`)
}).toThrow(ReferenceError)
})
it('should throw ReferenceError when accessing const before declaration', () => {
expect(() => {
eval(`
const before = y
const y = 20
`)
}).toThrow(ReferenceError)
})
it('should demonstrate that let IS hoisted by shadowing outer variable', () => {
const outer = "outer"
expect(() => {
eval(`
{
// If inner 'x' wasn't hoisted, this would access outer 'x'
// But we get ReferenceError, proving inner 'x' shadows outer from block start
const value = outer
let outer = "inner"
}
`)
}).toThrow(ReferenceError)
})
it('should work after declaration line is reached', () => {
let x
x = 10
expect(x).toBe(10)
const y = 20
expect(y).toBe(20)
})
it('should have separate TDZ for each block', () => {
let x = "outer"
{
// New TDZ for this block's x
let x = "inner1"
expect(x).toBe("inner1")
}
{
// Another new TDZ for this block's x
let x = "inner2"
expect(x).toBe("inner2")
}
expect(x).toBe("outer")
})
})
describe('Function Declaration Hoisting', () => {
it('should fully hoist function declarations', () => {
// Can call before declaration
const result = add(2, 3)
function add(a, b) {
return a + b
}
expect(result).toBe(5)
})
it('should hoist function declarations in any order', () => {
// Both functions can reference each other
function isEven(n) {
if (n === 0) return true
return isOdd(n - 1)
}
function isOdd(n) {
if (n === 0) return false
return isEven(n - 1)
}
expect(isEven(4)).toBe(true)
expect(isOdd(3)).toBe(true)
expect(isEven(3)).toBe(false)
expect(isOdd(4)).toBe(false)
})
it('should hoist function inside blocks (in non-strict mode behavior)', () => {
// Function declared inside block
function example() {
if (true) {
function inner() {
return "inner"
}
return inner()
}
}
expect(example()).toBe("inner")
})
})
describe('Function Expression Hoisting', () => {
it('should throw TypeError for var function expression called before assignment', () => {
expect(() => {
eval(`
greet()
var greet = function() { return "Hello" }
`)
}).toThrow(TypeError)
})
it('should throw ReferenceError for let/const function expression in TDZ', () => {
expect(() => {
eval(`
greet()
const greet = function() { return "Hello" }
`)
}).toThrow(ReferenceError)
})
it('should work after assignment for var function expression', () => {
var greet
expect(greet).toBe(undefined)
greet = function() {
return "Hello"
}
expect(greet()).toBe("Hello")
})
it('should work after declaration for const function expression', () => {
const greet = function() {
return "Hello"
}
expect(greet()).toBe("Hello")
})
})
describe('Arrow Function Hoisting', () => {
it('should throw ReferenceError for arrow function in TDZ', () => {
expect(() => {
eval(`
sayHi()
const sayHi = () => "Hi"
`)
}).toThrow(ReferenceError)
})
it('should work after declaration', () => {
const sayHi = () => "Hi"
expect(sayHi()).toBe("Hi")
})
it('should follow same rules as function expressions', () => {
// Arrow functions are always expressions
const multiply = (a, b) => a * b
const add = function(a, b) { return a + b }
expect(multiply(3, 4)).toBe(12)
expect(add(3, 4)).toBe(7)
})
})
describe('Class Hoisting', () => {
it('should throw ReferenceError when using class before declaration', () => {
expect(() => {
eval(`
const dog = new Animal("Buddy")
class Animal {
constructor(name) {
this.name = name
}
}
`)
}).toThrow(ReferenceError)
})
it('should work after class declaration', () => {
class Animal {
constructor(name) {
this.name = name
}
}
const dog = new Animal("Buddy")
expect(dog.name).toBe("Buddy")
})
it('should throw ReferenceError for class expression before declaration', () => {
expect(() => {
eval(`
new MyClass()
const MyClass = class {}
`)
}).toThrow(ReferenceError)
})
})
describe('Hoisting Precedence and Order', () => {
it('should have function declarations win over var initially', () => {
function example() {
const typeAtStart = typeof myValue
var myValue = "string"
function myValue() {
return "function"
}
const typeAtEnd = typeof myValue
return { typeAtStart, typeAtEnd }
}
const result = example()
// Function is hoisted over var initially
expect(result.typeAtStart).toBe("function")
// But var assignment overwrites it
expect(result.typeAtEnd).toBe("string")
})
it('should merge multiple var declarations', () => {
var x = 1
expect(x).toBe(1)
var x = 2
expect(x).toBe(2)
var x = 3
expect(x).toBe(3)
})
it('should hoist var without value if only declared later', () => {
function example() {
const first = x
var x
const second = x
x = 5
const third = x
return { first, second, third }
}
const result = example()
expect(result.first).toBe(undefined)
expect(result.second).toBe(undefined)
expect(result.third).toBe(5)
})
})
describe('Common Hoisting Pitfalls', () => {
it('should demonstrate the function expression trap', () => {
// This is the #1 hoisting mistake
function example() {
try {
return sum(2, 3)
} catch (e) {
return e.name
} finally {
// eslint-disable-next-line no-unused-vars
var sum = function(a, b) {
return a + b
}
}
}
expect(example()).toBe("TypeError")
})
it('should work when using function declaration instead', () => {
function example() {
return sum(2, 3)
function sum(a, b) {
return a + b
}
}
expect(example()).toBe(5)
})
it('should demonstrate var loop problem with closures', () => {
const funcs = []
for (var i = 0; i < 3; i++) {
funcs.push(function() {
return i
})
}
// All return 3 because they share the same hoisted 'i'
expect(funcs[0]()).toBe(3)
expect(funcs[1]()).toBe(3)
expect(funcs[2]()).toBe(3)
})
it('should fix loop problem with let', () => {
const funcs = []
for (let i = 0; i < 3; i++) {
funcs.push(function() {
return i
})
}
// Each iteration gets its own 'i'
expect(funcs[0]()).toBe(0)
expect(funcs[1]()).toBe(1)
expect(funcs[2]()).toBe(2)
})
})
describe('Test Your Knowledge Examples', () => {
it('Question 1: var hoisting returns undefined then value', () => {
function example() {
const results = []
results.push(x)
var x = 10
results.push(x)
return results
}
const [first, second] = example()
expect(first).toBe(undefined)
expect(second).toBe(10)
})
it('Question 2: let throws ReferenceError', () => {
expect(() => {
eval(`
console.log(y)
let y = 20
`)
}).toThrow(ReferenceError)
})
it('Question 3: var function expression throws TypeError', () => {
expect(() => {
eval(`
sayHi()
var sayHi = function() { console.log("Hi!") }
`)
}).toThrow(TypeError)
})
it('Question 4: function declaration works before definition', () => {
function example() {
return sayHello()
function sayHello() {
return "Hello!"
}
}
expect(example()).toBe("Hello!")
})
it('Question 5: function vs var same name', () => {
function example() {
var a = 1
function a() { return 2 }
return typeof a
}
expect(example()).toBe("number")
})
it('Question 6: inner const shadows outer due to hoisting', () => {
const x = "outer"
function test() {
// The const x below IS hoisted and creates TDZ from start of function
// So accessing x here tries to access the inner x which is in TDZ
try {
// eslint-disable-next-line no-unused-vars
const result = x // This x refers to inner x (TDZ!)
const x = "inner"
return result
} catch (e) {
return e.name
}
}
// The const x inside the try block shadows outer x due to hoisting
expect(test()).toBe("ReferenceError")
})
})
describe('Edge Cases', () => {
it('should handle nested function hoisting', () => {
function outer() {
const result = inner()
function inner() {
return deepest()
function deepest() {
return "deep"
}
}
return result
}
expect(outer()).toBe("deep")
})
it('should handle var in catch block', () => {
try {
throw new Error("test")
} catch (e) {
var caught = e.message
}
// var escapes the catch block
expect(caught).toBe("test")
})
it('should not hoist variables from eval in strict mode', () => {
// In strict mode, eval has its own scope
"use strict"
eval('var evalVar = "from eval"')
// evalVar is not accessible outside eval in strict mode
expect(typeof evalVar).toBe("undefined")
})
it('should hoist function parameters like var', () => {
function example(a) {
const typeAtStart = typeof a
var a = "reassigned"
const typeAfter = typeof a
return { typeAtStart, typeAfter }
}
const result = example(42)
expect(result.typeAtStart).toBe("number")
expect(result.typeAfter).toBe("string")
})
it('should handle default parameter TDZ', () => {
// Earlier parameters can be used in later defaults
function test(a = 1, b = a + 1) {
return a + b
}
expect(test()).toBe(3) // a=1, b=2
expect(test(5)).toBe(11) // a=5, b=6
expect(test(5, 10)).toBe(15) // a=5, b=10
})
it('should throw for later parameter used in earlier default', () => {
expect(() => {
eval(`
function test(a = b, b = 2) {
return a + b
}
test()
`)
}).toThrow(ReferenceError)
})
})
describe('Real-World Patterns', () => {
it('should enable module pattern with hoisted functions', () => {
function createCounter() {
// Variables must be declared before return (let/const don't hoist values)
// But functions ARE fully hoisted, so we can reference them before definition
let count = 0
return {
increment,
decrement,
getValue
}
// Function implementations below - these ARE hoisted
function increment() {
return ++count
}
function decrement() {
return --count
}
function getValue() {
return count
}
}
const counter = createCounter()
expect(counter.increment()).toBe(1)
expect(counter.increment()).toBe(2)
expect(counter.decrement()).toBe(1)
expect(counter.getValue()).toBe(1)
})
it('should demonstrate readable code structure with hoisting', () => {
// Public API at the top
function processUser(user) {
validate(user)
const formatted = format(user)
return save(formatted)
// Implementation details below
function validate(u) {
if (!u.name) throw new Error("Name required")
}
function format(u) {
return { ...u, name: u.name.toUpperCase() }
}
function save(u) {
return { ...u, saved: true }
}
}
const result = processUser({ name: "alice", age: 30 })
expect(result.name).toBe("ALICE")
expect(result.saved).toBe(true)
})
})
describe('Documentation Examples', () => {
describe('Why TDZ Exists (MDX lines 220-225)', () => {
it('should throw ReferenceError when inner let shadows outer, not access outer value', () => {
// This demonstrates WHY the TDZ exists - to prevent confusing behavior
// where you might accidentally reference an outer variable
const x = "outer"
function example() {
// Without TDZ, this might confusingly print "outer"
// With TDZ, JavaScript tells you something is wrong
try {
// eslint-disable-next-line no-unused-vars
const value = x // Tries to access inner x which is in TDZ
// eslint-disable-next-line no-unused-vars
let x = "inner"
return value
} catch (e) {
return e.name
}
}
// The inner let x shadows outer x from the start of the function
// so we get ReferenceError instead of "outer"
expect(example()).toBe("ReferenceError")
// But outer x is still accessible outside the function
expect(x).toBe("outer")
})
})
describe('Best Practices', () => {
describe('1. Declare variables at top of scope (MDX lines 542-554)', () => {
// This tests the processUser example from Best Practice 1
function processUser(user) {
// All declarations at the top
const name = user.name
const email = user.email
let isValid = false
// Logic follows
if (name && email) {
isValid = true
}
return isValid
}
it('should validate user with name and email', () => {
expect(processUser({ name: "Alice", email: "alice@example.com" })).toBe(true)
})
it('should invalidate user without email', () => {
expect(processUser({ name: "Alice" })).toBe(false)
})
it('should invalidate user without name', () => {
expect(processUser({ email: "alice@example.com" })).toBe(false)
})
})
describe('2. Prefer const > let > var (MDX lines 562-567)', () => {
it('should not allow const reassignment', () => {
expect(() => {
eval(`
const API_URL = 'https://api.example.com'
API_URL = 'https://other.com'
`)
}).toThrow(TypeError)
})
it('should allow let reassignment', () => {
let currentUser = null
currentUser = { name: "Alice" }
currentUser = { name: "Bob" }
expect(currentUser.name).toBe("Bob")
})
it('should allow var reassignment and redeclaration', () => {
var counter = 0
counter = 1
var counter = 2 // Redeclaration allowed with var
expect(counter).toBe(2)
})
})
describe('3. Use function declarations for named functions (MDX lines 577-583)', () => {
it('should hoist function declaration (calculateTotal can be called before definition)', () => {
// Function declaration is fully hoisted
const items = [{ price: 10 }, { price: 20 }, { price: 30 }]
const result = calculateTotal(items)
function calculateTotal(items) {
return items.reduce((sum, item) => sum + item.price, 0)
}
expect(result).toBe(60)
})
it('should work with arrow function after declaration (calculateTax)', () => {
const calculateTax = (amount) => amount * 0.1
expect(calculateTax(100)).toBe(10)
expect(calculateTax(250)).toBe(25)
})
it('should throw ReferenceError if arrow function called before declaration', () => {
expect(() => {
eval(`
calculateTax(100)
const calculateTax = (amount) => amount * 0.1
`)
}).toThrow(ReferenceError)
})
})
describe('5. Don\'t rely on hoisting for variable values (MDX lines 605-615)', () => {
it('should show bad pattern: var x is undefined before assignment', () => {
function bad() {
const valueBeforeAssignment = x // undefined - works but confusing
var x = 5
const valueAfterAssignment = x
return { before: valueBeforeAssignment, after: valueAfterAssignment }
}
const result = bad()
expect(result.before).toBe(undefined)
expect(result.after).toBe(5)
})
it('should show good pattern: const x is properly defined', () => {
function good() {
const x = 5
return x // 5 - clear and predictable
}
expect(good()).toBe(5)
})
})
})
})
})