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

Summary

In this lesson, you learned that:

  • A function is a named piece of code that you write once and can run as many times as you need. Thanks to functions, you don't have to repeat code, and when you change a function, the change applies everywhere it's used.
  • You define a function with the keyword function, a name, parentheses with parameters and a body in curly braces: function printArea(width, height) { ... }. A definition alone doesn't run anything.
  • You call a function with its name and parentheses: printArea(5, 6);. It's good practice to define functions first and call them below.
  • A parameter is a name in the definition, an argument is a value in the call. Arguments go into parameters in order. A parameter that gets no argument is undefined, unless it has a default value: function printPrice(price, currency = "EUR").
  • return gives a value back to the place of the call, so you can use the call like a value: console.log(add(2, 3)), const total = add(2, 3) * 2;. console.log only shows a value. A function without return returns undefined.
  • return ends the function right away, and the rest of the function doesn't run. With conditions, the first return that runs wins.
  • Variables created inside a function, and its parameters, exist only inside that function: they are local. Variables from outside can be used inside functions. To get a value out of a function, return it.
  • A function can take an array, go through it with a loop and return a result. The return goes after the loop.
  • Arrow functions are a shorter way to write functions: const add = (a, b) => a + b; returns the value after the arrow. With a body in curly braces, you need return: const area = (width, height) => { ... return ...; };.

In the next lesson, Objects, you will learn to keep values that belong together, such as a person's name and age, in a single value.

Instructions

  1. 1

    Let's practice. Write a function cartTotal with two parameters: prices (an array of prices) and discount with the default value 0. The function returns the sum of the prices minus the discount. Print cartTotal(cart) and cartTotal(cart, 50). The program prints 280 and 230.

  2. 2

    Write an arrow function formatPrice with a parameter price that returns the price as text with the currency, for example 230 EUR for 230. Use it to print the discounted total: formatPrice(cartTotal(cart, 50)).

Back

10/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.