javascript
javascript
Function Declaration vs Expression
Live
bolt
Level1
Progress0%

Function Declaration vs Expression

CARD 1

Introduction

Login to report note issues for this section.

Function Declaration vs Expression

A function is a reusable block of code designed to perform a specific task. Instead of writing the same logic multiple times, you define it once in a function and call it whenever needed.

codejs
1// Without a function — repeated code
2console.log(5 * 5);
3console.log(10 * 10);
4console.log(3 * 3);
5
6// With a function — reusable
7function square(n) {
8  return n * n;
9}
10
11console.log(square(5));  // 25
12console.log(square(10)); // 100
13console.log(square(3));  // 9

In JavaScript, there are two primary ways to define a function: function declaration and function expression. They look similar but behave differently in one critical way — hoisting.