1. Function declarations
A function declaration defines a named function using the function keyword. This is the most traditional way to create functions in JavaScript, and it's the first pattern you'll use constantly at UrbanCart.
When a customer adds items to their UrbanCart, we need a function to sum up all the prices. Instead of writing the same addition logic in 15 different places, we write it once as a function and call it whenever we need a total.
2. Function expressions
A function expression assigns an anonymous (unnamed) function to a variable. Unlike declarations, expressions are not hoisted โ you must define them before you call them.
Declaration
function name() {}
Hoisted. Can be called before definition. Named automatically.
Expression
const name = function() {}
Not hoisted. Must be defined first. Stored in a variable.
3. Arrow functions
Arrow functions (introduced in ES6) provide a shorter syntax for writing functions. They're perfect for small, concise operations โ exactly the kind of utility functions UrbanCart needs everywhere.
.map(), .filter(), .forEach()), and utility functions. Use regular function declarations for top-level named functions and methods that need their own this binding.
4. Parameters & defaults
Functions accept parameters โ placeholders for the data you pass in when calling the function. ES6 introduced default parameters, which provide fallback values when no argument is supplied.
undefined โ not when it's null or 0. Passing applyDiscount(1000, 0) uses 0, not the default.
5. Return values
Functions return data using the return keyword. Once return executes, the function stops immediately โ any code after it won't run. If you omit return, the function returns undefined.
6. Scope
Scope determines where a variable is accessible. JavaScript has three scope levels: global, function, and block. Understanding scope prevents bugs and keeps your UrbanCart code predictable.
Accessible everywhere
Inside function only
Inside { } only
const or let (never var). Use const by default, let only when you need to reassign. This keeps scope tight and prevents accidental global variables.
7. Callbacks intro
A callback is a function passed as an argument to another function. The receiving function "calls back" your function at the right time. This is a foundational pattern in JavaScript โ you'll use it with array methods, event handlers, and async operations throughout UrbanCart.
products.filter(function(p) { return p.price > 2000; }) vs products.filter(p => p.price > 2000) โ same result, half the code.
Try It Yourself #1
Create three UrbanCart utility functions: calculateTotal(), applyDiscount(), and formatPrice(). Run them with the sample cart data to see the results.
Try It Yourself #2
Build a mini order processor that chains functions together. Calculate the subtotal, apply a member discount, add shipping, and format the final output โ all using functions you write.
Test your knowledge
applyDiscount(1000) return if the function is defined as function applyDiscount(price, discount = 0.1) { return price * (1 - discount); }?let inside a function is accessible where?๐ Challenge: Build a coupon system
Put your function skills to the test. Build a coupon system for UrbanCart using everything you learned:
- Create a
validateCoupon(code)function that checks if a coupon code is valid (e.g., "SAVE10", "WELCOME20", "FLAT500") - Create a
getCouponDiscount(code, subtotal)function that returns the discount amount โ percentage coupons use a rate, FLAT500 subtracts a fixed โน500 - Create an
applyCoupon(cart, code)function that chains everything: calculates subtotal, validates coupon, applies discount, and returns a formatted receipt string - Use default parameters, arrow functions, and callbacks where they make the code cleaner
Bonus: Add a forEach callback to print each cart item in the receipt.
You just mastered JavaScript functions.
Now build a full-stack career.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ