Basic JavaScript - Return a Value from a Function with Return

Tell us what’s happening:
function timesFive(num) {
return num * 5;
}
console.log(timesFive(5))

In previous questions regarding function parameters and arguments, the console.log was placed within the curly bracket. Why is console out side the curly brackets here?

Your code so far

function timesFive(num) {
  return num * 5;
}
 console.log(timesFive(5))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Return a Value from a Function with Return

Link to the challenge:

function timesFive(num) {
  console.log(num * 5);
}

Is this what you mean? This function does not return anything and only prints the output.

function timesFive(num) {
  return num * 5;
}
 console.log(timesFive(5))

On the other hand, this function returns the output (but not print it) so you have to use console.log() outside the function in order to see the return value of the function.

The console log is calling the function so it is placed outside of it.

You can place console logs in different parts of the code for different reasons. If you want to check a local variable in a function the console log would have to be placed inside the function then you would have to call the function to see the results.

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