Function Declarations, Expressions, and Arrow Functions

Beginner · 9 min

Function declarations, expressions, and arrow functions do the same job, but differ in when they exist, hoisting, and what `this` means inside them.

Before thisValues, Types, and Coercion

PortEXE · 9 min

Function Declarations, Expressions, and Arrow Functions

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PortEXEReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • There are three ways to create a function in JavaScript — a declaration (`function greet() {}`), an expression (`const greet = function () {}`), and an arrow function (`const greet = () => {}`) — and all three produce something callable, but they differ in when they exist and how `this` works.
  • Function declarations are hoisted completely: the engine registers the name and the full function body before any code in that scope runs, so you can call a declared function above the line where it's written.
  • Function expressions and arrow functions are not hoisted that way — they're just values assigned to a variable, so they only exist once execution actually reaches that assignment.
  • Arrow functions don't create their own `this` — they borrow it from whatever scope they're written inside, which is why they're the natural choice for callbacks that need to reach outward. The full mechanics of `this` are the next lesson's job.
  • Arrow functions also drop their own `arguments` object and can't be used as constructors, which is part of why they're a distinct tool rather than a drop-in replacement for the other two forms.

One capability, three syntaxes

Every function in JavaScript does the same basic job — it’s a block of code you can run more than once, optionally with inputs and an output. What changes across the three ways to write one isn’t what it can do, but when it starts existing and how it decides what this means inside it. Those two differences sound minor until you hit a bug that only makes sense once you know which form you’re looking at: a “function not defined” error on a line that’s clearly above the function’s definition, or a callback that mysteriously loses track of the object it was supposed to be working with.

Function declarations: fully hoisted

function greet(name) {
  return `Hello, ${name}`;
}

This is a function declaration — the function keyword starts the statement, and the function gets a name directly. Before the engine runs a single line of the surrounding scope, it scans ahead and registers every declared function in full: name and body both. That’s why this works even when the call comes first in the file:

console.log(greet("Ada")); // works fine
function greet(name) { return `Hello, ${name}`; }

This full hoisting is a real convenience — it means you can organize a file with the main logic up top and helper functions below it, read in whatever order makes the file easiest to follow, not the order the engine happens to need them in.

Function expressions: a value, not a registration

const greet = function (name) {
  return `Hello, ${name}`;
};

Here the function has no independent existence — it’s just a value, created by a function expression, sitting on the right side of an assignment to greet. Nothing about that value is hoisted. If greet was declared with var, the variable name itself is hoisted (as undefined), but calling it before the assignment line throws, because there’s no function there yet. With let or const, the variable isn’t even accessible before that line. Either way, a function expression only becomes callable once execution reaches the statement that creates it — this is the ordering problem the video for this lesson walks through directly, comparing it side by side with a declaration so the difference actually shows up on screen instead of staying abstract.

Arrow functions: shorter syntax, borrowed this

const greet = (name) => `Hello, ${name}`;

An arrow function is a particular kind of function expression — same hoisting story as above — but with a syntax built for brevity and one behavioral difference that matters a lot in practice: it has no this of its own. Whatever this means in the scope where the arrow function is written is what this means inside it, permanently. A regular function gets a fresh this determined by how it’s called — and that’s exactly the mismatch that causes a classic bug: pass a method as a callback, and a regular function loses the object it was attached to, while an arrow function keeps it automatically, because it was never looking there in the first place. That’s worth flagging now and leaving alone for a lesson — the this keyword, call, apply, and bind is where it gets the full treatment it deserves.

Arrow functions also drop a couple of things a regular function has: their own arguments object, and the ability to be used as a constructor with new. Neither is a loss in practice — they’re signals that an arrow function is meant for short, scoped-in operations (array callbacks, event handlers that need the outer this), not as a general-purpose function form.

Where this fits

This lesson is the foundation for the rest of the “Functions and this” group. Default, rest, and spread parameters builds on whichever of these three forms you’re already writing; the full rules for this and the explicit-binding tools (call, apply, bind) assume you already know why arrow functions behave differently, which is the one idea from this lesson worth not skimming past.

Check yourself

  1. 01Why can you call a function declared with `function foo() {}` before the line it's written on, but not one written as `const foo = function () {}`?

    A function declaration is hoisted in full — name and body both — so the engine has already registered it before running any code in that scope. A function expression is just a variable assignment; the variable name may be hoisted (if declared with `var`) but the function value isn't attached to it until execution reaches that line, so calling it earlier fails.

  2. 02What's the practical difference between a function expression and an arrow function beyond the shorter syntax?

    An arrow function has no `this`, `arguments`, or `super` of its own — it reads all three from its enclosing scope, and it can't be used with `new`. A function expression gets its own `this` determined by how it's called, has its own `arguments` object, and can act as a constructor.

  3. 03If you need a callback that refers to `this` from the surrounding method, which form should you reach for, and why?

    An arrow function, because it doesn't rebind `this` to whatever called it — it keeps the `this` from the scope where it was written. A regular function expression or declaration used as that callback would get its own `this`, usually not the one you wanted.

  4. 04Is an arrow function ever hoisted the way a function declaration is?

    No. An arrow function is always an expression assigned to a variable, so it follows that variable's hoisting rules (undefined until the line runs, for `var`; unreachable before it, for `let`/`const`), never the full-hoisting behavior reserved for function declarations.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark