JavaScript Basics: How to return a value from a function in Vanilla JavaScript

This time I want to talk what a return statement does and how you can return a value from a function in Vanilla JavaScript.

If you don’t like to watch a video, then scroll down for reading (almost) everything I said in the video.

Let’s start with creating a simple multiply function in Vanilla JavaScript.

function getMultiply(numberParam) {
  console.log('Multiply', numberParam * numberParam)
}

We can call it with:

getMultiply(7)

So now this function will multiply the number 7. But we can’t do anything with the outcome of it.

Return a value from the function

Let’s bring in the return statement.

function getMultiply(numberParam) {
  return numberParam * numberParam
}

Now we have to call the function in the console.log to see the outcome.

console.log(getMultiply(7));

If we put this function into a variable, we have the outcome stored inside of it!

With the return statement, you can get value out of the function. For example you can return true, false or an Object or Array.