davidatthepark davidatthepark - 2 months ago 5
Javascript Question

Difference between Immutable.js List() and List.of()?

I'm reading through the Immutable.js docs for creating lists with

List()
and
List.of()
and I don't understand what the difference is. Any help with understanding this would be great.

Answer

List.of() uses each parameter as a separate element in the created list, while List() takes a single parameter of some kind of iterable-like structure.

Usage:

List.of("a", "b", "c"); // ["a","b","c"]

List(["a","b","c"]); // ["a","b","c"]

The ... notation in the definition List.of<T>(...values: T[]): List<T> is meant to indicate a variable number of arguments, which will be treated like an array internally, probably using the arguments object.

Comments