Loading
0x40Lesson 5 of 6

Write reusable functions

Bundle instructions into named functions that accept inputs and return results.

12 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Declare and call a function
  • Pass arguments
  • Return a result to the caller

A function is a named set of instructions. Parameters are its named inputs; arguments are the values supplied when you call it.

double.js
1function double(number) {
2  return number * 2;
3}
4
5const answer = double(6);
6console.log(answer);
Output
12

return sends a value back to the caller and ends that function call. It is different from console.log, which only displays a value.

Key takeaways

  • Functions name reusable behavior.

  • Arguments provide values for parameters.

  • return gives a result back to the caller.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write JavaScript

Write the program, run it against the test cases, and use the feedback to build your JavaScript skills.

Exercise 1

Double it

+25 XP

Write a function named double that returns twice its number argument. Print the result for the number in input.

  • Double 3
  • Double 14
main.js
Loading editor…

JavaScript runs in a sandboxed browser frame with a 1.5 second time limit. Input is available as the string variable input.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: