Skills practised: Functions, Function declarations and expressions.
04Explanation
A function is a named block of code you can run whenever you need it. Write it once, call it many times.
A function declaration starts with function, a name, brackets and a body: function showMenu() { ... }. Defining it does nothing visible; calling it with showMenu() runs the body.
A function expression stores a function in a variable: const showMenu = function () { ... };. You call it the same way.
Name functions with verbs that say what they do: printReceipt, calculateTax, isValidEmail. Good names let the code read like a list of steps.
Function declarations are hoisted with their body, so they can be called before the line where they appear. Function expressions assigned to const/let are in the temporal dead zone until that line runs.
Functions are values (first-class): they can be stored, passed as arguments and returned. This is the foundation of callbacks, array methods and event handlers.
A named function expression (const f = function inner() {}) gives better stack traces and allows recursion through the inner name.
Keep functions small and focused on one task. If you need "and" to describe a function, it may be two functions.
05Definitions
Function
A reusable block of code that runs when it is called.
Function declaration
A statement that defines a named function: function name() {}.
Function expression
A function created as a value, often stored in a variable.
Call (invoke)
Running a function by writing its name followed by brackets.
Hoisting
Function declarations are available before their line in the code.
A shop prints the same footer on receipts, emails and invoices. Moving it into one function means a new phone number is changed once and appears everywhere.
function printFooter() {
console.log("Thanks for shopping!");
}
printFooter();
12Knowledge check
1. How many times is Hi printed?
function hi() { console.log("Hi"); }
hi();
hi();
0
1
2
3
2. What does defining a function do?
Runs it immediately
Stores it so it can be called later
Prints it
Deletes old code
3. Why is nothing printed?
function show() {
console.log("Shown");
}
The function has no name
It is never called
console.log is wrong
Functions cannot print
4. Which can be called before its line in the code?
A function declaration
A const function expression
Both
Neither
5. What is printed?
const f = function () {};
console.log(typeof f);
function
string
undefined
object
6. Which function name is best?
doStuff
calculateTax
x
function1
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Call the function: Prints Doors open
Receipt header function: Output unchanged; printHeader is a function; Header text written once
Store hours helpers: Prints both lines; One declaration and one expression
ex-021-01: output matches the documented expected output
ex-021-02: output matches the documented expected output
ex-021-03: output matches the documented expected output
ex-021-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-05-functions-and-reusable-logic/lesson-021-function-declarations-and-expressions/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
A function is defined once and called many times.
Declarations: function name() {}. Expressions: const name = function () {}.
Calling needs brackets.
Declarations are hoisted; const expressions are not.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationFunctions
Context: I am new to functions.
Task: Explain defining versus calling with a receipt example.
Constraints: Beginner level.
Expected behaviour: I can explain why functions reduce repetition.
Testing: Give me three programs to refactor.
Code predictionCall order
Context: I know how to declare and call functions.
Task: Give me five programs and ask the order of their output lines.
Constraints: Wait for answers.
Expected behaviour: Include hoisting.
Testing: Explain any I miss.
DebuggingFunction does nothing
Context: My function prints nothing.
Task: Ask questions that lead me to the problem.
Constraints: No direct answer.
Expected behaviour: I fix it myself.
Testing: List the three most common causes.
Exercise generationRefactoring practice
Context: I finished the receipt header exercise.
Task: Create two programs with repeated code for me to turn into functions.
Constraints: Exact output must stay the same.
Expected behaviour: Each has at least two repeated blocks.
Testing: Include the expected output.
Code reviewFunction names
Context: Here are my function names.
Task: Review them and suggest clearer verb-based names.
Constraints: Explain each suggestion.
Expected behaviour: Every name says what the function does.
Testing: Give me a naming checklist.