denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.95k stars 2.55k forks source link

Cannot access 'i' before initialization #123

Closed Ice-Hazymoon closed 5 years ago

Ice-Hazymoon commented 5 years ago
window.i = 1
console.log(i)
let i = 2
console.log(i)

// Uncaught ReferenceError: Cannot access 'i' before initialization at <anonymous>:1:3
Ice-Hazymoon commented 5 years ago

image


image

joshabolade commented 4 years ago
window.i = 1
console.log(i)
let i = 2
console.log(i)

// Uncaught ReferenceError: Cannot access 'i' before initialization at <anonymous>:1:3

You have to re-write your code you used the variable i and then defined it define it first then use it.

let i = 2 window.i = 1 console.log(i)