Overview

5 First-class functions for the novice: definitions and arguments

JavaScript places functions at the center of the language, treating them as first-class objects rather than mere blocks of reusable code. This means functions can be stored in variables, placed in arrays or object properties, passed into other functions, and returned from them. The chapter opens by stressing that understanding functions is essential to writing more powerful, elegant JavaScript, and it introduces the main ways to create them: function declarations, function expressions, arrow functions, method definitions, class fields, and the Function constructor. It also explains immediate function expressions, which must be wrapped in syntax that makes them unmistakably expressions rather than declarations.

Beyond syntax, the chapter shows how flexible functions become when they are used as values. It explores callbacks, where one function is passed to another and invoked later, such as in event handlers or array sorting. It also demonstrates practical uses of function behavior, including storing unique functions in a Set, attaching metadata like display names for debugging, and memoization, where a wrapper caches prior results to avoid repeating expensive work. These examples reinforce the idea that functions are not just callable units, but versatile building blocks that support cleaner APIs and more modular designs.

The chapter then turns to the details of function inputs and how JavaScript and TypeScript handle them. It distinguishes parameters from arguments, explains what happens when there are too many or too few values supplied, and introduces rest parameters for collecting extra arguments and default parameters for supplying fallback values. Finally, it shows how TypeScript improves function safety and clarity through parameter and return type annotations, overload signatures, and TSDoc comments. Together, these tools help catch bad calls earlier, document intent more clearly, and make function-heavy code easier to maintain and understand.

A function declaration in JavaScript.
A comparison of a standard function call and an immediate call to a function expression
The syntax of an arrow function
The flow of execution when making the useless(getText) call. The useless function is called with getText as an argument. In the body of the useless function is a call to the passed-in function, which in this case triggers the execution of the getText function (we’ve “called back” to the getText function).
The difference between function parameters and function arguments
Arguments are assigned to function parameters in the order specified. Excess arguments aren’t assigned to any parameters.

Summary

  • Writing sophisticated code hinges upon learning JavaScript as a functional language.
  • Functions are first-class objects that are treated just like any other objects within JavaScript. Similar to any other object type, they can be
    • Created via literals
    • Assigned to variables or properties
    • Passed as parameters
    • Returned as function results
    • Assigned properties and methods
  • There are different types of functions, most notably function declarations, function expressions, arrow functions.
  • Function declarations must have a name, and must be placed as separate statements in our code.
  • Callback functions are functions that other code will later “call back,” and are often used, especially with event handling.
  • The versatility of functions opens up some interesting possibilities; for example
    • We can add functions to collections, including arrays and sets.
    • We can attach properties to functions for use in debugging tools.
    • We can use a memoization function to wrap a function with additional code that performs caching.
  • A parameter is a variable that we list as a part of a function definition, whereas an argument is a value that we pass to the function when we invoke it.
  • A function’s parameter list and its argument list can be different lengths:
    • Unassigned parameters evaluate as undefined.
    • Extra arguments aren’t bound to parameter names.
  • Rest parameters and default parameters are new additions to JavaScript:
    • Rest parameters enable us to reference the remaining arguments that don’t have matching parameter names.
    • Default parameters enable us to specify default parameter values that will be used if no value is supplied during function invocation.
  • TypeScript allows you to specify types and validate function calls before your code runs.
    • Return types are always optional, but defining them can help you catch bugs.
    • You can overload a function with multiple type signatures.
  • TSDoc provides a standardized format for documenting function arguments and return values, and the TypeScript compiler bundles those comments with type information for the benefit of anyone consuming your code.

FAQ

What are the main ways to define a function in JavaScript?JavaScript supports several function forms, including function declarations, function expressions, arrow functions, method definitions, class fields, and the Function constructor. This chapter focuses mainly on declarations, expressions, and arrow functions.
What is the difference between a function declaration and a function expression?A function declaration is a standalone statement that starts with the function keyword and a name. A function expression is part of another expression, such as an assignment or a function argument, and its name is optional.
Why do function declarations need a name?Function declarations must have a name because they are invoked by that name. Since they are complete statements, JavaScript requires them to be named.
What is an immediately invoked function expression (IIFE)?An IIFE is a function expression that is created and called right away, such as (function() {})(). Parentheses or unary operators are used so the parser treats it as an expression rather than a declaration.
Why are arrow functions considered shorter or more concise?Arrow functions let you omit the function keyword, and for simple expressions you can omit braces and the return keyword too. For example, name => "Greetings " + name is a compact equivalent of a function expression.
What does it mean that functions are “first-class objects” in JavaScript?It means functions can be treated like any other object: assigned to variables, stored in arrays or object properties, passed as arguments, returned from other functions, and given their own properties.
What is a callback function?A callback is a function passed to another function or registered with an API so that it can be called later. Common examples include event handlers and comparator functions used by sort.
What is the difference between a parameter and an argument?A parameter is the named variable in a function definition. An argument is the value supplied when the function is called.
How do rest parameters work in a function?Rest parameters use ... before the last parameter name to collect all remaining arguments into an array. For example, function multiMax(first, ...remainingNumbers) puts extra arguments into remainingNumbers.
How can TypeScript help catch bad function calls?TypeScript can check parameter types, infer return types, support overload signatures, and document functions with TSDoc. This helps catch incorrect calls at compile time instead of waiting for runtime errors.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Secrets of the JavaScript Ninja, Third Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Secrets of the JavaScript Ninja, Third Edition ebook for free