Scope Question (Unit 4.6: Scope--Block Scope II)


#1

image

can someone explain why this code logs both ‘Northern Lights’ and ‘Moonlight’?
i expected it to log only northern lights?


#2

lightWaves defined on line 5 only exist within the if block, so line 6 will print northern lights

so after the if block (line 8) the value will again be Moonlight, given this is outside the scope of lightWaves defined of line 5


#3

Thanks that makes sense!!


#4

it has to do with the scope, the let varible is first declared with one value and then printed out… then it changes back to the orignal value you declared…


#5

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