TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 9 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript Functions: Declarations, Parameters, return

A function is a reusable block of code with a name, inputs (parameters) and an output (the return value). Functions are also values in JavaScript: you can store them in variables, pass them to other functions and return them. That idea powers the array methods and promises you will meet later.

Function declarations

function greet(name = "friend") { ... } declares a function named greet with one parameter. The = "friend" part is a default value, used when the caller passes nothing or passes undefined. Calling greet("Emma") runs the body with name set to "Emma".

return sends a value back and stops the function. If a function ends without return, or with a bare return;, the caller receives undefined, as noReturn() shows.

Rest parameters

A parameter written ...nums collects all remaining arguments into a real array, so sum(1, 2, 3, 4) works with any number of arguments. It must be the last parameter. Older code used the special arguments object for this; rest parameters replace it.

JavaScript · editable

Expected output

Hello, Emma!
Hello, friend!
10
81
undefined
declarations are hoisted

Function expressions

const square = function (x) { return x * x; }; creates a function as part of an expression and stores it in a variable. It behaves like a declaration once defined, with one key difference: hoisting.

Hoisting

Function declarations are hoisted: the whole function is created before any code in its scope runs, which is why hoisted() can be called above the line that defines it. Function expressions assigned to const or let are not available before their line; calling them early throws a ReferenceError. Many teams define functions before using them anyway, for readability.

Arguments are passed by value

Primitives such as numbers and strings are copied into parameters, so changing a parameter does not affect the caller's variable. Objects and arrays are passed as references to the same object, so a function that pushes to an array parameter changes the caller's array.

Common mistakes

  • Forgetting return, then wondering why the result is undefined.
  • Putting return on its own line with the value on the next: automatic semicolon insertion ends the statement and returns undefined.
  • Confusing greet (the function value) with greet() (calling it).

Summary

  • Declare with function name(params) { }; call with name(args).
  • Default parameters apply when an argument is missing or undefined.
  • ...rest collects extra arguments into an array.
  • Declarations are hoisted; function expressions are not.

Quiz: Functions

1. What does a function return when it has no return statement?

Without return, the result of a call is undefined.

2. In function f(a, ...rest), what is rest for f(1, 2, 3)?

The rest parameter collects the arguments after a into an array.

3. Which can be called before the line where it is defined?

Function declarations are hoisted with their body.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.