Defining and Calling Functions in JavaScript
Functions package reusable logic under a name, declared with function add(a, b) or as expressions (const add = function...). Calling runs the body with arguments bound to parameters, then resumes after the call β missing arguments arrive as undefined, extras are ignored (available via rest). Return values hand results back; falling off the end returns undefined.
JavaScript functions are first-class values: assignable, passable, returnable β the foundation of callbacks, higher-order functions, and functional patterns throughout the language.
Declaration anatomy
function add(a, b) { return a + b; } β name, parameters, body; return hands a value back.
Call and resume
add(2, 3) binds a=2, b=3, computes, and returns 5 to the call site expression.
Missing means undefined
add(2) leaves b undefined (NaN on use) β arity is never enforced, only conventional.
First-class values
Functions assign to variables and pass as arguments β the gateway to callbacks and composition.
function add(a, b) { return a + b;}console.log(add(2, 3));Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.