Write reusable functions
Bundle instructions into named functions that accept inputs and return results.
- 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.
1function double(number) {
2 return number * 2;
3}
4
5const answer = double(6);
6console.log(answer);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.
returngives 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.
Double it
Write a function named double that returns twice its number argument. Print the result for the number in input.
- Double 3
- Double 14
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…