Block Scope II


#1

I don’t understand the example exercise:

const colorOfSky = () => {
  const dusk = true;
  let color = 'blue'; 
  if (dusk) {
    let color = 'pink';
    console.log(color); // pink
  }
  console.log(color); // blue 
};

colorOfSky(); // blue
console.log(color); // ReferenceError

I don’t understand why when we call the function in the result it doesn’t show pink and blue? I see dusk is true so why just blue appears when we call it? Why pink it’s not showing?

Thanks so much!
SImona


#2

it shows pink and blue, i ran your code here:

http://jsbin.com/fapomatovo/edit?js,console

there is a run button in top right corner, it runs fine?

functions only executed when called


#3

Thank you. The code above was took from the lesson. Maybe you should update it in there so the explanation would be correct.
I thought I had it wrong, thinking that the 2 colors should appear. Thank you, again.


#4

in my case, two colors do appear. Please provide a screenshot for clarification.


#5

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