5.5.1 Top-level functions

    Examples:

    1. /** @param {string} str */
    2. const processString = (str) => {
    3. // Process the string.
    4. exports = {processString};

    5.5.2 Nested functions and closures

    Functions may contain nested function definitions. If it is useful to give thefunction a name, it should be assigned to a local const.

    5.5.3 Arrow functions

    Arrow functions provide a concise function syntax and simplify scoping thisfor nested functions. Prefer arrow functions over the function keyword,particularly for nested functions (but see??).

    Prefer arrow functions over other this scoping approaches such asf.bind(this), goog.bind(f, this), and const self = this. Arrow functionsare particularly useful for calling into callbacks as they permit explicitlyspecifying which parameters to pass to the callback whereas binding will blindlypass along all parameters.

    The left-hand side of the arrow contains zero or more parameters. Parenthesesaround the parameters are optional if there is only a single non-destructuredparameter. When parentheses are used, inline parameter types may be specified(see ).

    Tip: Always using parentheses even for single-parameter arrow functions canavoid situations where adding parameters, but forgetting to add parentheses, mayresult in parseable code which no longer works as intended.

    The right-hand side of the arrow contains the body of the function. By defaultthe body is a block statement (zero or more statements surrounded by curlybraces). The body may also be an implicitly returned single expression ifeither: the program logic requires returning a value, or the void operatorprecedes a single function or method call (using void ensures undefined isreturned, prevents leaking values, and communicates intent). The singleexpression form is preferred if it improves readability (e.g., for short orsimple expressions).

    Disallowed:

    1. /**
    2. * A function with no params and no returned value.
    3. * not require returning a value and we're missing the `void` operator.
    4. */
    5. const moduleLocalFunc = () => anotherFunction();

    5.5.4 Generators

    Generators enable a number of useful abstractions and may be used as needed.

    When defining generator functions, attach the to the function keyword whenpresent, and separate it with a space from the name of the function. When usingdelegating yields, attach the to the yield keyword.

    Example:

    5.5.5 Parameter and return types

    Function parameters and return types should usually be documented with JSDocannotations. See ?? for more information.

    5.5.5.1 Default parameters

    Optional parameters are permitted using the equals operator in the parameterlist. Optional parameters must include spaces on both sides of the equalsoperator, be named exactly like required parameters (i.e., not prefixed withopt_), use the = suffix in their JSDoc type, come after required parameters,and not use initializers that produce observable side effects. All optionalparameters for concrete functions must have default values, even if that valueis undefined. In contrast to concrete functions, abstract and interfacemethods must omit default parameter values.

    Example:

    1. /**
    2. * @param {string} required This parameter is always needed.
    3. * @param {string=} optional This parameter can be omitted.
    4. * @param {!Node=} node Another optional parameter.
    5. function maybeDoSomething(required, optional = '', node = undefined) {}
    6. /** @interface */
    7. /**
    8. * Interface and abstract methods must omit default parameter values.
    9. * @param {string=} optional
    10. */
    11. someMethod(optional) {}
    12. }

    Note: Unlike Python's default parameters, it is okay to use initializers thatreturn new mutable objects (such as {} or []) because the initializer isevaluated each time the default value is used, so a single object won't beshared across invocations.

    Tip: While arbitrary expressions including function calls may be used asinitializers, these should be kept as simple as possible. Avoid initializersthat expose shared mutable state, as that can easily introduce unintendedcoupling between function calls.

    5.5.5.2 Rest parameters

    Use a rest parameter instead of accessing arguments. Rest parameters aretyped with a prefix in their JSDoc. The rest parameter must be the lastparameter in the list. There is no space between the and the parametername. Do not name the rest parameter var_args. Never name a local variable orparameter arguments, which confusingly shadows the built-in name.

    Example:

    5.5.6 Generics

    Declare generic functions and methods when necessary with @template TYPE inthe JSDoc above the function or method definition.

    5.5.7 Spread operator

    Function calls may use the spread operator (). Prefer the spread operatorto Function.prototype.apply when an array or iterable is unpacked intomultiple parameters of a variadic function. There is no space after the .

    Example:

    1. function myFunction(...elements) {}