Is a console.log() the same thing as a return statement?


#1

Question

Is a console.log() the same thing as a return statement?

Answer

No! console.log() will print whatever we pass into the parentheses to our development console, while a return statement will return a value to the function it’s associated with. To log the return value of a function call we can do something like this:

const myFunction () => {
  return 'Hello world!';
}
console.log(myFunction()); //will print 'Hello world!' to the development console because 'Hello world!' is the return value for the function

To log something from inside a function:

const anotherFunc () => {
  console.log('Yo world!'); //will print 'Yo world!' to the console when the function is called 
}
anotherFunc();

However if we use console.log() to log the return value of a function that contains a console.log() and no return statement:

const yetAnotherFunc () => {
  console.log('Hi world!'); //will print 'Hi world!' to the console when the function is called 
}
console.log(yetAnotherFunc()); //because no return value has been specified for the function, we'll also see `undefined` logged to the console after 'Hi world!'