Manuals / JavaScript / Chapter 3

A · Fundamentals · beginner · Week 1–2 · Chapter 3 of 14

Functions & scope

Functions are first-class: assign them, pass them, return them. Arrow functions vs function declarations. Scope (block vs function), hoisting intuition, and default parameters. Pure functions — same input, same output, no side effects — are the foundation of testable code.

Path progress
16%

Step 1 of 5

Three ways to write functions

Declaration: function foo() {} — hoisted. Expression: const foo = function() {} — not hoisted. Arrow: const foo = () => {} — concise, no own this (important later).

Three ways to write functionsDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Three ways to writdrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

function isEven(n) { return n % 2 === 0 }

const isEvenExpr = function(n) { return n % 2 === 0 }

const isEvenArrow = (n) => n % 2 === 0

Do this now

Write isEven(n) three ways. Verify all return the same results.

Was this step clear?
Chapter learning outcomes
  • Function declarations vs expressions
  • Arrow functions
  • Parameters & defaults
  • Block scope
  • Return early pattern

Clear these before you leave

Side quest

String utilities module

Create utils.js with titleCase, truncate(str, max), and slugify(str). Test each in Node or console.