JavaScript · Lesson 5 · 14 min
Functions — Reusable Recipes
Bundle steps into a function you can call whenever you need it.
You will learn
- Define a function with function
- Pass values in with parameters
- Call a function and use its return value
A function is a named recipe. Write it once, use it many times. Functions can take inputs (parameters) and give back a result (return value).
Defining and calling
function greet(name) { ... } creates the recipe. greet("Sam") runs it with name set to "Sam".
Return values
return sends a value back to whoever called the function. The add example below returns the sum of two numbers.
Try it yourself
Output
Press Run code to see results here.
Tip: Write a function double(n) that returns n * 2 and log double(7).