Chaining #

Math.js supports chaining operations by wrapping a value into a Chain. A chain can be created with the function math.chain(value) (formerly All functions available in the math namespace can be executed via the chain. The functions will be executed with the chain’s value as the first argument, followed by extra arguments provided by the function call itself.

    .done() // 5

math.chain( [[1, 2], [3, 4]] )
    .subset(math.index(0, 0), 8)
    .done() // [[24, 6], [9, 12]]


A Chain is constructed as:


The Chain has all functions available in the math namespace, and has a number of special functions:

Note that a “rest” or “…” parameter may not be broken across the value in the chain and a function call. For example

math.chain(3).median(4,5).done() // throws error

does not compute the median of 3, 4, and 5.

Fork me on GitHub