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 put anything out of the function.

If you want to return true, false or an Object or Array.

How do you use the return statement?

If you have another way of using the statement return? Please share it in the comments!🙏 But if you have any problems with returning a value out of a JavaScript function? Please let me help you, post your question in the comments!


Also published on Medium.

Author Raymon S

Lead Frontend Developer ♥ CSS & Sass / Founder of Mr Frontend Community / Coach & Mentor- #HTML #Sass #BEM #JavaScript #AngularJS #responsive #CoderDojo #NodeJS

More posts by Raymon S
%d bloggers like this: