Scope/Block Scope II why on earth the function without "return" returns a value?


#1

I am referring to the example on that page:
https://www.codecademy.com/courses/learn-javascript-scope/lessons/scope/exercises/block-scope-ii?action=resume_content_item&course_redirect=introduction-to-javascript

The example is like that:

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); // undefined

Why on earth the function colorOfSky is supposed to return “blue” while it has no “return” inside and basically no idea what is its result?


#2

color is a local variable and not visible outside of the function.


#3

That’s pretty clear. But why then blue in the comment?

 colorOfSky(); // blue

#4

Because the function is logging the value. No return value needed since the task is completed by the function.


#6

That’s it. I have been confused by the comment //blue - thought it is about colorOfSky() function, in fact it is about color variable. Now clear, thanks


#7

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