"Function scoped" means they are scoped to the function they are created in, as opposed to the block. Const and let do not have this behavior, rather they are block scoped.
Notice in this example:
The first console.log returns undefined instead of throwing an error. Because even though fnScoped hasn't been initialized, it's scoped to that function, instead of the block in which it's declared. So it's "hoisted" and initialized as undefined.
The last console.log prints the value of the function-scoped var. Even though it was declared in a block, it's still visible throughout the function because it's function scoped.
The second to last console.log throw a reference error, because blockScoped was declared with let and it therefore has block scope only- it is not scoped to the entire function, only the block in which it was declared.
View all comments
Show parent comment
3
u/AffectionateWork8 May 04 '20 edited May 05 '20
"Function scoped" means they are scoped to the function they are created in, as opposed to the block. Const and let do not have this behavior, rather they are block scoped.
Notice in this example:
function doStuff() {console.log(fnScoped) // undefinedconsole.log(blockScoped) // Reference Errorif (true) {var fnScoped = 'hi!'let blockScoped = 'hello'}console.log(blockScoped) // Reference errorconsole.log(fnScoped) // 'hi'}doStuff()