Im having trouble understanding the difference between these two blocks of code and why one works vs the other. I saw these code snippets in a talk going over the Temporal Dead Zone in JS, but was having a hard time understanding this particular case with default parameters.
Throws a ReferenceError
// sample1.js
const a = 2;
function square(a = a) {
return a * a;
}
// Does not work!
square();
Works
// sample2.js
const init = 2;
function square(a = init) {
return a * a;
}
// Works!
square(); // => 4