Functions package reusable logic. Declarations are hoisted; expressions assign functions to variables.
Forms
- Declaration:
function add(a, b) { return a + b; } - Expression:
const add = function(a, b) { ... } - Parameters default:
function greet(name = 'guest')
Pure functions
Same inputs → same outputs, no side effects—easier to test and reason about.
Important interview questions and answers
- Q: Hoisting?
A: Declarations available before line in scope—expressions not until assigned. - Q: Return undefined?
A: Functions without return yield undefined.
Self-check
- Difference declaration vs expression?
- What is a side effect?
Tip: Re-run the playground code for functions-declarations and tweak one line before the MCQs.
Interview prep
- Hoisting?
Declarations available throughout their scope.