Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

addEventListener
A call on an element that registers a listener for one kind of event.
closest
A way to find the nearest parent of an element that matches a rule you give, starting with the element itself.
closure
A function together with the variables from the place it was written, which stay available when it runs later.
default behaviour
What the browser does on its own when an event happens, such as following a link or submitting a form, before any script intervenes.
DOM
The tree of objects a browser builds from the page, which a script can read and change while the page is open.
element
One item in the page tree, such as a heading or a button, which a script can find and change.
event
Something that happens on the page, such as a click, a key press, or a form being submitted, which a script can respond to.
event bubbling
The way an event rises from the element that received it up through each of its parents.
event target
The element that actually received the action, which may be a child of the element whose listener is running.
function
A reusable block of code that can be called with values and can return an answer to whoever called it.
handler
The function you give to a listener, which the browser runs when the event it waits for happens.
listener
A function registered on an element to wait for one kind of event and run each time that event reaches it.
property
A named value stored on an object, usually read and written with a dot, such as the text of an element.
reference
A value that points at an object rather than holding a copy, so a change made through one is visible through the other.
scope
The part of a program where a name can be used, decided by where it was declared.
variable
A name that holds a value so you can use it again; a constant cannot be pointed at a different value later.

JavaScript from the ground up

Functions

A name for a piece of work: parameters in, a return value out, and arrow functions for the short ones.

Part 4 of 12

Beginner14 min readBack to Courses

A name for a piece of work

A function is a block of code with a name, written once and called as often as needed. function greet(name) { … } declares it; greet("Ada") runs it with that value in place of the parameter.

Everything the function needs arrives through its parameters, and what it produces leaves through return. A function that returns its sentence can be logged, stored or combined; one that only logs can do nothing else.

Declared, then called

function greeting(name) {
  return `Hello, ${name}!`;
}

console.log(greeting("Ada"));
console.log(greeting("Grace"));

Arrow functions

The shorter form — const double = (n) => n + n; — is a function as a value: a name on the left, parameters, and an expression that is returned without the word return. For one-line functions it is the common shape in modern JavaScript.

With a block body the braces and the return come back. Use the arrow form for the short, obvious functions and the full form when a reader benefits from seeing the shape — the language does not mind either way.

Small pieces are easier to fix

The gain is the same as in any language: a function that does one thing can be read, tried and corrected on its own. If the honest name needs the word "and", it is two functions that have not been separated yet.

Parameters, defaults and scope

A parameter can have a default: function greet(name = "friend") uses the fallback when the caller passes nothing or undefined. That keeps the function usable with one argument and explicit with two, without a second overload or a branch at every call site.

Names declared inside a function belong to that function. A const created in the body is gone when the call returns, and a name outside it is visible unless the function shadows it. That isolation is why a function can be moved without dragging the rest of the file with it.

A function that logs and a function that returns are different tools. console.log(greeting("Ada")) works because greeting returns a value; if it only logs, the outer call prints undefined. Decide which one the caller needs before writing the body.

Pure functions are easier to trust

A function that only reads its arguments and returns a value is called pure. It has no hidden state, so the same input always gives the same output, and a test can call it without setting up a page. Most of the functions worth naming in a program can be written this way; the ones that touch the DOM or log are the exceptions, and they are easier to find when they are isolated.

A worked call, step by step

Follow console.log(greeting("Ada")). The argument "Ada" is evaluated first and bound to the parameter name. The body builds a template literal with that value and returns it. The call is replaced by the returned string, and only then does console.log run, printing the sentence. The function itself does not print anything.

That order explains the difference between returning and logging. A function that returns hands the value back to the expression that called it, so it can be logged, stored, compared or passed on. A function that logs has already used the value up; the caller receives undefined and can do nothing with it.

Returning early

A return also ends the function immediately. That is useful for a guard at the top: if a name is empty, return the fallback and let the rest of the body assume a real value. The reader can then stop thinking about the empty case at the line where it was handled.

Without an early return, the same guard becomes a nested if around the whole body, and each extra condition adds a level of indentation. A short function with two guards at the top is usually easier to read than one with a single deeply nested branch.

Try these next

  • Write add(a, b) as both a declaration and an arrow function; call each once.
  • Give a greeting function a default name and call it with no argument.
  • Return a value from a function and log it outside, instead of logging inside.

A sentence from two values

Write a function that takes a name and a number and returns a sentence about them. Call it twice with different values and log both results.

Live Preview
Console output will appear here...

3 left to do

  • A function is declared
  • It hands a value back
  • It is called and logged

function sentence(name, count) { return …; } — then two console.log(sentence(…)) calls.

The same work, in arrow form

Rewrite the greeting as an arrow function with a default name, call it with and without an argument, and log both results.

Live Preview
Console output will appear here...

3 left to do

  • An arrow function is used
  • The parameter has a default
  • Both calls are logged

const greet = (name = "friend") => …; returns the expression directly. Call it as greet() and greet("Ada").

A function with no return gives back undefined, which logs as the word undefined. That is usually the whole explanation for an unexpected "undefined" in the console.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What is the difference between return and console.log inside a function?