Scope Block Scope - undefined

https://www.codecademy.com/courses/introduction-to-javascript/lessons/scope/exercises/block-scope

When logged this is the output that occurs.

Moonlight
undefined
/home/ccuser/workspace/scope-block-scope/main.js:7
console.log(lightWaves)
^

ReferenceError: lightWaves is not defined
at Object. (/home/ccuser/workspace/scope-block-scope/main.js:7:13)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

I am wondering WHY I goet the undefined code in the output as this isn’t something that I am understanding and have had it happen in other strings of code before.

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  console.log(lightWaves);
}

console.log(logVisibleLightWaves())
console.log(lightWaves)

here:

console.log(logVisibleLightWaves())

you log the return value of the logVisibleLightWaves function, but logVisibleLightWaves doesn’t return anything so undefined indicates the absence of a return value

1 Like

Interesting, can you give me the way you may do this so you may not have to be able to see the undefined prompt?

I would assume the logVisibleLightWaves() call alone but even then there is no return value.

that is then simple? Don’t attempt to log the return value.

1 Like

This line will throw an error. You declared lightWaves inside your logVisibleLightWaves function. lightWaves is not defined outside the scope of the function. It seems to me that what you are trying to do is something like this:

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  return lightWaves; //This returns your desired output to the caller.
}

console.log(logVisibleLightWaves()) //console.log is the caller, so the return value will be logged to the console.

Output:

Moonlight

You could also do it this way:

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  console.log(lightWaves); //Your desired output gets logged to the console here.
}

logVisibleLightWaves(); //Just call the function. Don't log it to the console.

Output:

Moonlight

Hope this helps!

1 Like

Done and corrected, so for clarity you are saying that the console.log() fx is meant for return fx’s… ust clarifying…

console.log() will log or ‘print’ whatever is inside the parenthesis to the console. When you put a call to a function inside the parenthesis, whatever comes back from the function will be logged or ‘printed’ to the console.
In this example, the function returns a value, so console.log(logVisibleLightWaves()) logs that value to the console.

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  return lightWaves;
}

console.log(logVisibleLightWaves());

Output:

Moonlight

In this example, there is no return statement. When a function doesn’t have a return statement, the value ‘undefined’ is implicitly returned. The function itself contains a console.log(), so that value is logged to the console. Then ‘undefined’ is returned to here console.log(logVisibleLightWaves());, and ‘undefined’ is logged to the console.

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  console.log(lightWaves); //Your desired output gets logged to the console here.
}

console.log(logVisibleLightWaves());

Output:

Moonlight
undefined

Hope this helps!