Home

Arrayfrom

Array.from is a static method of the Array object in JavaScript. It creates a new array from an array-like or iterable object. It accepts two optional parameters: mapFn and thisArg.

If the first argument is a string, an arguments object, a NodeList, or any object with a

If a mapFn is provided, it is called for each element with arguments (value, index, array) and

Examples:

- Array.from('hello') yields ['h','e','l','l','o'].

- Array.from({length: 3}, (_, i) => i) yields [0, 1, 2].

- Array.from(document.querySelectorAll('div')) yields an array of the selected elements (in environments with a DOM).

If the provided object is neither iterable nor array-like, Array.from throws a TypeError. It is widely supported

length
property
and
indexed
elements,
a
new
array
is
produced
with
the
corresponding
elements.
If
the
first
argument
is
iterable
(implements
Symbol.iterator),
its
elements
are
iterated
to
fill
the
array.
This
makes
Array.from
capable
of
converting
both
traditional
array-like
structures
and
modern
iterables
into
real
arrays.
its
return
value
is
used
to
populate
the
new
array.
If
thisArg
is
supplied,
it
will
be
used
as
the
this
value
when
invoking
mapFn.
This
enables
customized
transformations
during
the
conversion
process.
in
modern
browsers
and
Node.js;
older
environments
can
use
a
polyfill.
In
practice,
Array.from
complements
the
spread
operator
and
provides
a
single,
consistent
API
for
creating
arrays
from
diverse
inputs.