Block Scope in JS


Why does my code give different answers when I use let inside the if and when I do not?

const visibleLightWaves=() => {
  let lightWaves='Moonlight';
  let region='The Arctic';
  if(region==='The Arctic')
    {let lightWaves='Northern Lights'
   // lightWaves='Northern Lights'



because using let will create a new variable with a block scope (within the if clause, so everything between the {} of if)

without using let, it will update the existing lightWaves variable


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.