JavaScript Functions

js_functions.html

About Functions

Functions can be defined either using function declarations or function expressions.

Function expressions can be stored in variables, can be used as anonymous functions (e.g. as function parameters), and can be returned by higher-order functions.

Callbacks: functions as parameters

Like any other object and every primitive, functions can be passed to other functions as parameters.

Array Functions

JavaScript offers a range of functions to transform arrays. The outputs of these transformations are either arrays or primitives. The most important array functions are:

Function: map()

Syntax:

array.map(callback(currentValue [, index]));


Callback parameters:

Function: filter()

Syntax:

array.filter(callback(currentValue [, index]));


Callback parameters:
The callback’s return value is interpreted as a true or false.

Function: reduce()

Syntax:

array.reduce(callback(accumulator, currentValue [, index]), initValue);


Callback parameters:

More about JS