01 / 21
\(\textcolor{#c084fc}{f}(\textcolor{#f472b6}{x})\)
return ;

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

function greet ( name ) { return "Hello, " + name ; } keyword name parameter function body return value

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

Time Compilation Phase Declarations registered in memory Execution Phase Code runs line by line function add() ✓ Available immediately ✓ Fully hoisted Can call before definition const add = function() ✕ Not available before ✕ Not hoisted Must define before calling

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

function add(a, b) { ... } Parameters Placeholders in the definition add(3, 5); Arguments Actual values passed in the call a = 3 b = 5

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?

Question 2 of 5

What happens when this code runs?

Question 3 of 5

What is the value of result?

Question 4 of 5

What does console.log(message) output?

Question 5 of 5

Which statement about arrow functions is correct?