0

I am trying to learn pure javascript and stumbled upon the topic of let and var keyword. Based on my understanding, let has a local scope and var has global scope even if it is declared in an inner block of code.

The problem was in the following code.

let glob='global'

if(true){
  var local='local'
  console.log(local)
  glob='inlocal'
  console.log(glob)
  let glob='local'
  console.log(glob)

}

console.log(local)
console.log(glob)

The above gives the error 'glob is not defined' at the line glob='inlocal' As the glob is declared in the outer block, shouldn't it value be updated?

Also, when I commented the lines

let glob='local'
console.log(glob) 

There was no error for the code. I don't understand why this effect is there.

rahul valluri
  • 15
  • 1
  • 4

0 Answers0