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?

Outline

Go Pro?

If you upgraded to pro, sign in here

  • About
  • Blog
  • Privacy
Looking to email me? You can get me on my first name at allthecode.co