Functions
Write functions with parameters and return values, in declaration and arrow form.
18 min
A function is a reusable piece of code with a name. Define it once, call it as often as you like.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Ada")); // Hello, Ada!nameis a parameter; the value you pass in a call is an argument.returnsends a value back and ends the function. A function with noreturngives backundefined.console.logshows a value to a human;returngives it to the code that called.
A shorter form you will see everywhere:
const double = (x) => x * 2;
const add = (a, b) => a + b;
const area = (w, h) => {
const result = w * h;
return result;
};With one expression and no braces, the value is returned automatically. With braces you must write return yourself.
function greet(name, greeting = "Hello") {
return `${greeting}, ${name}!`;
}
greet("Ada"); // "Hello, Ada!"
greet("Ada", "Hi"); // "Hi, Ada!"You can store a function in a variable, put it in an array, or pass it to another function. That last idea powers map, filter and event handlers, which you meet soon:
function twice(fn, x) { return fn(fn(x)); }
twice(double, 3); // 12“A function that logs the result has returned it.”
You see the answer on screen, so it feels delivered.
What is actually true
console.log only prints. Without return, the function gives back undefined, so double(4) + 1 is NaN and the answer cannot be stored or reused.
How to catch yourself
If you see `undefined` or `NaN` where a number belongs, look for a missing `return`.
double(x) should return twice its input. Fix it so double(4) + 1 works.
What it does now
double(4) + 1 prints NaN: the function gives back undefined, and undefined + 1 is NaN.
Your code must satisfy
- double(4)→8four
- double(0)→0zero
- double(-3)→-6negative
- double(4) + 1→9add one
function double(x) {
console.log(x * 2);
}Write isLeapYear(year) that returns true for leap years and false otherwise.
A year is a leap year if it divides by 4, except years that divide by 100, unless they also divide by 400. So 2024 and 2000 are leap years; 1900 and 2023 are not.
Your code must satisfy
- isLeapYear(2024)→trueordinary leap
- isLeapYear(2023)→falsenot divisible by 4
- isLeapYear(1900)→falsecentury, not leap
- isLeapYear(2000)→trueevery 400
- isLeapYear(2100)→falseanother century
function isLeapYear(year) {
}Write an arrow function clamp that takes (x, low, high) and returns x limited to the range: low if below it, high if above it, otherwise x.
Your code must satisfy
- clamp(5, 0, 10)→5inside
- clamp(-4, 0, 10)→0below
- clamp(99, 0, 10)→10above
- clamp(10, 0, 10)→10edge
const clamp = (x, low, high) => {
};- A function takes parameters and
returns a value; with noreturnit gives backundefined. - Arrow functions
(a, b) => a + bare a short form; add braces andreturnfor several statements. - Default parameters (
greeting = "Hello") fill in missing arguments. - Functions are values: you can pass them to other functions.
Function checks.
What does a function without return give back?
- A0
- Bnull
- Cundefined
- D""
const f = (a, b = 10) => a + b; What is f(5)?
Which arrow function returns the square of x?
- A`(x) => { x * x }`
- B`(x) => x * x`
- C`x => return x * x`
- D`=> x * x`
const add = (a, b) => a + b; What is add(add(1, 2), 3)?
The keyword that sends a value back to the caller is .
Capitalisation, spacing and punctuation are ignored when grading.
Which repeated job in your own life could be a function with parameters? Name it, say what goes in and what comes out.
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of JavaScript for Beginners is planned around your week. Free.