30 Days Of JavaScript

Day 5: Functions Part 1

Lesson 4: Input, Return

In the last lesson we saw that functions can receive an input argument, well they can also return a value as well.

In this playground you'll see "Hello Cool Coder" logged twice. The first time from within the sayHello function and the second time in the main part of the code. This is where we assign the return from the function to a const called message and then print that out.

Let's break down what's going on here:

  • The function receives the input name and creates helloMessage by concatenating "Hello " with name.

  • The function then prints that message out

  • On the final line of the function helloMessage is returned. This means that helloMessage will now be accessible in the main body of code

  • We can get that returned value from sayHello(usersName) and store it in a const, in this case message, and then use it further on in our code

Here's another, slightly more realistic example:

What about if we want more than one input?


Go Pro?

Upgrade to Pro for quizzes, tracked progress and a completion certificate for just $25 🚀

Want more developer tips, tricks and tools?
Then follow me:
FREE Coding Career Quick Start Guide 🚀
Discover the best way to learn to code, how to land a job and valuable resources to help your journey in this free 15 page value packed guide.
Looking to email me? You can get me on my first name at allthecode.co