Function Declarations
& Expressions
Learn the two core ways to define and call functions in JavaScript: declarations and expressions, and when to use each.
JavaScript
Programming
What Is a Function?
A function is a reusable block of code designed to perform a particular task. Instead of writing the same logic over and over, you write it once inside a function and call it whenever you need it.
Define
Write the instructions once and give them a name.
Call
Execute those instructions whenever you need the result.
Reuse
Call the same function many times with different inputs.
Anatomy of a Function
Why Do Functions Matter?
Functions are the building blocks of every JavaScript program. They help you write code that is organized, readable, and maintainable.
1
DRY Principle: "Don't Repeat Yourself." Write once, call many times.
2
Abstraction: Hide complex logic behind a simple name.
3
Testability: Isolated blocks are easier to test and debug.
4
Composition: Small functions combine to build large features.
The Function Declaration
JS
function add(a, b) { return a + b; } // Call the function let result = add(3, 5); // 8
The Function Expression
JS
const multiply = function(a, b) { return a * b; }; // Call the function let result = multiply(4, 7); // 28
Arrow Function Shorthand
JS
// Arrow function expression const subtract = (a, b) => { return a - b; }; // Concise body (implicit return) const double = (n) => n * 2; double(6); // 12
Declaration vs Expression
Declaration
// Works! (hoisted) sayHi(); function sayHi() { return "Hi!"; }
Expression
// Error! (not hoisted) sayHi(); const sayHi = function() { return "Hi!"; };
How Hoisting Works
How to Write & Call a Function
1
Choose a style: Declaration if you need hoisting, expression if you want to assign to a variable.
2
Name it clearly: Use a verb: calculateTotal, getUserName, isValid.
3
Define parameters: List the inputs the function needs in parentheses.
4
Write the body: Place your logic inside curly braces { }.
5
Return a value: Use return to send back the result (optional).
6
Call it: Write the function name followed by ( ) with arguments.
Example 1: Temperature Converter
JS
// Declaration: convert Fahrenheit to Celsius function toCelsius(fahrenheit) { return (fahrenheit - 32) * 5 / 9; } // Call with an argument let temp = toCelsius(212); console.log(temp); // 100
Example 2: Expression with Default
JS
// Expression with a default parameter const greet = function(name = "World") { return "Hello, " + name + "!"; }; console.log(greet("Alice")); // "Hello, Alice!" console.log(greet()); // "Hello, World!"
Example 3: Arrow Functions in Practice
JS
// Arrow function to filter even numbers const isEven = (num) => num % 2 === 0; const numbers = [1, 2, 3, 4, 5, 6]; const evens = numbers.filter(isEven); console.log(evens); // [2, 4, 6]
Parameters vs Arguments
Common Mistakes
✕ Calling before expression
Using a function expression before it is defined. Unlike declarations, expressions are not hoisted.
✕ Missing return
Forgetting the return keyword. Without it, the function returns undefined by default.
✕ Forgetting parentheses
Writing myFunc instead of myFunc(). Without (), you reference the function object, not its result.
Summary Comparison
| Declaration | Expression | Arrow | |
|---|---|---|---|
| Syntax | function name() {} | const name = function() {} | const name = () => {} |
| Hoisted? | ✓ Yes | ✕ No | ✕ No |
| Named? | Always named | Can be anonymous | Always anonymous |
| Best for | Top-level logic | Callbacks, closures | Short callbacks |
| Has own this? | ✓ Yes | ✓ Yes | ✕ No (inherits) |
Question 1 of 5
Which of the following correctly defines a function declaration in JavaScript?
A function declaration starts with the function keyword followed by a name, parameters, and a body. Options A, C, and D are all function expressions assigned to variables.
Question 2 of 5
sayHello();
const sayHello = function() {
return "Hello!";
};
▼ Tap to read code
What happens when this code runs?
Function expressions are not hoisted. Calling sayHello before its definition causes a ReferenceError because the variable is in the temporal dead zone (declared with const).
Question 3 of 5
const square = (n) => n * n; let result = square(7);
▼ Tap to read code
What is the value of result?
The arrow function square uses a concise body that implicitly returns \( n \times n \). When called with 7, it computes \( 7 \times 7 = 49 \).
Question 4 of 5
function greet(name) {
"Hello, " + name + "!";
}
let message = greet("Sam");
console.log(message);
▼ Tap to read code
What does console.log(message) output?
The function body computes the string but never uses return. Without a return statement, the function returns undefined by default. Always remember to return the value you want to send back.
Question 5 of 5
Which statement about arrow functions is correct?
Arrow functions inherit the this value from their enclosing scope. They do not create their own this binding, which makes them ideal for callbacks but unsuitable as object methods that need their own this.