Basic JavaScript - Return a Value from a Function with Return

Tell us what’s happening:

Describe your issue in detail here.
How does “return” differ from “console.log()”? Could I have written what is below as such? Sorry for the silly question; I was just trying to understand the difference. Thank you!

function plusThree(num) {
  console.log( num + 3);
}

const answer = plusThree(5);

Your code so far

function plusThree(num) {
  return num + 3;
}

const answer = plusThree(5);

Your browser information:

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

Challenge Information:

Basic JavaScript - Return a Value from a Function with Return

1 Like

console.log() just prints to console, it doesn’t make function return anything. When function doesn’t return explicitly anything, the value returned by it will be undefined.

You can add at the end another printout to the console, to see that in the first case that’s undefined.

console.log('returned:', answer);
4 Likes

Thank you for the detailed explanation!

2 Likes

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