Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Returning Values

The function printArea you wrote a moment ago prints the area, and that's all. But what if we want to keep calculating with the result, for example add up the areas of all the rooms in an apartment? A printed value is only shown in the console; the program can't work with it anymore.

That's why functions can return a value. The keyword return sends a value back to the place where the function was called:

function add(a, b) {
    return a + b;
}

const sum = add(2, 3);
console.log(sum);

This prints:

5

The call add(2, 3) runs the function, and the function returns 5. You can imagine that JavaScript replaces the call with the value the function returned, so const sum = add(2, 3); becomes const sum = 5;. The value a function returns is called its return value.

You can use a call anywhere you would write a value: print it straight away, or calculate with it:

function add(a, b) {
    return a + b;
}

console.log(add(2, 3));
const total = add(2, 3) * 2;
console.log(total);

This prints:

5
10

In console.log(add(2, 3)), JavaScript first calls add(2, 3), and then prints the 5 the function returned. In add(2, 3) * 2, the call becomes 5, and 5 * 2 is 10.

Beginners often mix up return and console.log. console.log only shows a value to you, in the console. return gives the value to the program, which can store it, calculate with it or print it. Look at what happens when a function prints its result instead of returning it:

function printSum(a, b) {
    console.log(a + b);
}

const result = printSum(2, 3);
console.log(result);

This prints:

5
undefined

The 5 was printed by the console.log inside the function. But the function didn't return anything, and a function without return returns undefined. So result is undefined. That's why most functions return their result, and the code that calls them decides what to do with it.

Instructions

  1. 1

    Complete the function rectangleArea so that it returns width * height. The program then prints Kitchen: 12 and Bedroom: 20.

  2. 2

    The apartment also has a living room of 6 × 5 meters. Create a variable livingRoom with its area, calculated with the function rectangleArea. Then print the area of the whole apartment, kitchen + bedroom + livingRoom, with the label "Whole apartment:". It should be 62.

Back

5/10

  1. 1. Defining Functions
  2. 2. Calling Functions
  3. 3. Parameters and Arguments
  4. 4. Default Parameter Values
  5. 5. Returning Values
  6. 6. Return Ends the Function
  7. 7. Variable Scope
  8. 8. Functions and Arrays
  9. 9. Arrow Functions
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.