jQuery .toArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.toArray() returns a native JavaScript Array of every DOM element in the jQuery set (since jQuery 1.4). It takes no arguments. The array holds the same live nodes — not jQuery wrappers and not clones.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$(selector).toArray()  → Array of DOM elements

jQuery set  →  [el0, el1, el2, …]
               native Array (document order)

then use: reverse(), map(), filter(), for…of
.get() with no args ≈ same idea
.empty selection → []
CallWhat it does
jQuery setSource collection of matched elements
ArgumentsMethod takes no parameters
ReturnNew Array of DOM elements
SincejQuery 1.4

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Build a jQuery set: $("li"), $(".item"), etc.
Step 2Call .toArray() with no arguments.
Step 3Use Array methods on the result (reverse, map, …).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Convert a Selection to an Array

Get a native Array of matched elements and inspect length / tags.

jQuery
var items = $("li").toArray();

console.log(items.length);           // 3
console.log(Array.isArray(items));   // true
console.log(items[0].tagName);       // LI
Try It Yourself

How It Works

  1. Select. $("li") builds a jQuery set of list items.
  2. Convert. .toArray() returns a real Array.
  3. Inspect. Length is 3; first node’s tagName is LI.

Example 2 — Reverse with Array Methods

Official pattern: convert, reverse, then read text from each node.

jQuery
var divs = $("div.demo").toArray().reverse();
var texts = [];

for (var i = 0; i < divs.length; i++) {
  texts.push(divs[i].textContent);
}

console.log(texts.join(" "));  // Three Two One
Try It Yourself

How It Works

  1. toArray. Get [One, Two, Three] as DOM nodes.
  2. reverse. Native Array method reorders to [Three, Two, One].
  3. Read text. Loop pushes each node’s textContent.

Example 3 — map() Text from Nodes

Use Array map after converting the set.

jQuery
var labels = $("li").toArray().map(function (el) {
  return el.textContent;
});

console.log(labels.join(", "));  // Apple, Banana, Cherry
Try It Yourself

How It Works

  1. Convert. toArray() yields DOM elements.
  2. map. Each element becomes its text string.
  3. Result. A plain string array — easy to join or send to an API.

Frequently Asked Questions

.toArray() returns a standard JavaScript Array of all DOM elements in the jQuery set, in document order. It takes no arguments. Available since jQuery 1.4.
.get() with no arguments also returns an array of matched elements — same idea as .toArray(). .get(index) returns one DOM element. Prefer .toArray() when you clearly want a native array; use .get(n) for a single element.
Plain DOM Element nodes, not jQuery wrappers. Wrap again with $(node) if you need jQuery methods.
To use Array methods such as reverse(), map(), filter(), or for…of, or to pass the list to APIs that expect a real Array.
An empty array: []. For example $("#missing").toArray() is [].
No. The new Array holds references to the same live DOM nodes. Changing a node in the array changes the element in the page.

Next: .toggle() Method

Learn how to show and hide elements with a single toggle call.

.toggle() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful