(function() {
let val = 10;
console.log(val); // 10
})() // executed immediately
console.log(val); // val is not defined
VS
{
let val = 10;
console.log(val); //10
} // executed immediately
console.log(val) // val is not defined
Both code snippets seem to have the same effect. Can these two approaches be used interchangeably? Am i missing something?