This site runs best with JavaScript enabled.
Do you love what we do?Become a patronat Patreon❤️

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

Raymon S


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.

https://youtu.be/CXScWfSd-9Y

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!

Do you love what we do?Become a patronat Patreon❤️
Share article

Subscribe to the Mr Frontend weekly newsletter

Every week I will send you a email to help you: stay motivated, learn new things and the best 3 article's on the web