JavaScript Tutorial: Functions
Function declarations, expressions, arrow functions, callbacks, default parameters, rest, and pure functions.
The building blocks of everything
Functions are how you name a piece of logic, reuse it anywhere, and keep code DRY (Don't Repeat Yourself). JavaScript has three ways to write them — and they have meaningful differences around `this`, hoisting, and when to use each.
What you'll learn in this JavaScript functions tutorial
This interactive JavaScript tutorial has 9 hands-on exercises. Estimated time: 22 minutes.
- Three ways to write a function — JavaScript has three function syntaxes. They are mostly interchangeable — but they differ in hoisting and `this` binding…
- Arrow functions — compact syntax — Arrow functions have shortcuts based on how much they do:
- Parameters and return — Functions can take multiple parameters and return any value. A function with no `return` statement returns `undefined`.
- Default parameters — Default values are used when an argument is not provided (or is `undefined`):
- Rest parameters — any number of args — `...rest` collects remaining arguments into an array:
- Callbacks — functions as arguments — Functions are values in JavaScript — you can pass them as arguments to other functions.
- Pure functions — same input, same output — A **pure function** always returns the same result for the same inputs and has no side effects (doesn't modify anything …
- IIFE — run once immediately — An IIFE (Immediately Invoked Function Expression) runs the moment it's defined:
- Build a validation library — Final challenge — no starter code.
JavaScript Functions concepts covered
- The building blocks of everything