Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Return a Value or Log a Message?

A function has two ways to send a value out:

When a function only logs, nothing comes through the front door, so JavaScript puts undefined in the call’s place. It’s the same undefined a missing argument gets, JavaScript’s value for “nothing here.” This file prints 12 from inside the call, then undefined:

Language: JavaScript Filename: scratch/points.js
function showPoints(hits) {
  console.log(hits * 3);
}

const score = showPoints(4);
console.log(score);

Seeing 12 on the screen doesn’t mean the function handed 12 back.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 So far, every function you’ve written logs its answer. The exercise wrappers you filled in earlier used return instead, and this video shows you the difference.

00:10 Here’s one for the arcade game. Every hit is worth three points, so showPoints() logs the number of hits times three. Run the file with node, and four hits come out as 12 in the terminal.

00:24 Nice, that’s handy for checking your work. Now you’d like to give the player a bonus of five points. For that, your program needs the 12 itself, not a line of text on the screen.

00:35 Can this function hand it over?

00:39 A function has two ways to send a value out. Picture a function as a little house, with a side window and a front door. console.log() is the side window.

00:50 The value flies out to the terminal, where you can read it. The rest of your program never gets it. return, the word from those exercise wrappers, is the front door.

01:02 The value travels back to the line that called the function, and it takes the call’s place. If that line stores the call in a variable, the variable gets the value.

01:13 Here’s the file with one change. It stores the call in score, in a line like that one, then logs score. showPoints() still uses only its side window.

01:23 Your turn: pause the video and predict what the terminal shows.

01:32 First, 12. That line was logged inside the call, through the side window. Then undefined. Nothing came through the front door, so JavaScript put undefined in the call’s place.

01:47 It’s the same undefined a missing argument gets, JavaScript’s value for “nothing here”. Watch out for this one. Seeing 12 on the screen doesn’t mean the function handed 12 back.

02:01 To hand the value back, put return where console.log() was. It doesn’t show anything anymore, so rename it countPoints().

02:10 Now the call turns into 12, and score holds 12. The logging moves out to the calling code, which adds the bonus and logs the result. Run it, and you get 17.

02:22 Watch out, though. return doesn’t show anything by itself. Without this console.log() line, the file would print nothing.

02:33 So a function has two exits. console.log() shows a value to you. return hands it back to your code, where you can keep using it.

02:45 Next, in Use What a Function Returns, you’ll return answers from decisions and use returned values in your own calculations.

Become a Member to join the conversation.