Skip to content
Lesson 3 of 10 · JavaScript for Beginners

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.

javascript5 lines
function greet(name) {
  return "Hello, " + name + "!";
}

console.log(greet("Ada"));   // Hello, Ada!
  • name is a parameter; the value you pass in a call is an argument.
  • return sends a value back and ends the function. A function with no return gives back undefined. console.log shows a value to a human; return gives it to the code that called.

A shorter form you will see everywhere:

javascript6 lines
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.

javascript5 lines
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:

javascript2 lines
function twice(fn, x) { return fn(fn(x)); }
twice(double, 3);    // 12
Common misconception

“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`.

Exercise4 checks
javascript

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
starter code3 lines
function double(x) {
  console.log(x * 2);
}
Exercise5 checks
javascript

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
starter code3 lines
function isLeapYear(year) {
  
}
Exercise4 checks
javascript

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
starter code3 lines
const clamp = (x, low, high) => {
  
};
  • A function takes parameters and returns a value; with no return it gives back undefined.
  • Arrow functions (a, b) => a + b are a short form; add braces and return for several statements.
  • Default parameters (greeting = "Hello") fill in missing arguments.
  • Functions are values: you can pass them to other functions.
Check yourself

Function checks.

1Multiple choice

What does a function without return give back?

  • A0
  • Bnull
  • Cundefined
  • D""
2Numeric answer

const f = (a, b = 10) => a + b; What is f(5)?

3Multiple choice

Which arrow function returns the square of x?

  • A`(x) => { x * x }`
  • B`(x) => x * x`
  • C`x => return x * x`
  • D`=> x * x`
4Numeric answer

const add = (a, b) => a + b; What is add(add(1, 2), 3)?

5Fill the blanks

The keyword that sends a value back to the caller is .

Capitalisation, spacing and punctuation are ignored when grading.

For you · never graded

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.