Block Scope in JS


#1

https://www.codecademy.com/courses/learn-javascript-scope/lessons/scope/exercises/block-scope-ii?action=lesson_resume&course_redirect=introduction-to-javascript

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'
    console.log(lightWaves);
    }
  console.log(lightWaves);
}

visibleLightWaves();



#2

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


#3

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