jQuery .toArray() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .toArray()?

.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.

Remember
$(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 → []
NeedUse
Native array of all matches$els.toArray()
One element by index$els.get(i)
Array-like → real Array (any object)$.makeArray(obj)

How to Use .toArray()

Select elements, convert, then use Array APIs.

  1. Build a jQuery set: $("li"), $(".item"), etc.
  2. Call .toArray() with no arguments.
  3. Use Array methods on the result (reverse, map, …).
  4. Wrap a node again with $(node) if you need jQuery methods.
PieceRequired?Role
jQuery setYesSource collection of matched elements
ArgumentsNoneMethod takes no parameters
Return—New Array of DOM elements
Since—jQuery 1.4

Pseudocode

Pseudocode
toArray():
    result = new Array
    for each element in this jQuery set (document order):
        result.push(element)   // same node reference
    return result

// empty set → []

Cheat Sheet

Keep these facts handy when bridging jQuery and Array APIs.

TopicDetail
Syntax.toArray()
ReturnsNative Array of DOM nodes
OrderDocument order (same as the set)
Empty set[]
vs .get().get() ≈ all; .get(i) = one
Clones?No — references to live nodes
SincejQuery 1.4

Live Preview

Convert a small jQuery set of list items and try Array methods on the result.

Browser demo of .toArray() — same idea as the examples below.

Result
length → 3
tags → LI, LI, LI
Array.isArray → true

/* native Array of DOM elements */

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.
  4. Not a jQuery object. items has Array methods, not .css().

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.
  4. Join. Output shows the reversed labels.

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.
  4. Note. jQuery also has .map(); here we use the Array version on purpose.

Example 4 — .toArray() vs .get()

No-arg .get() matches .toArray(); indexed .get(i) returns one node.

jQuery
var $li = $("li");

console.log($li.toArray().length);  // 3
console.log($li.get().length);      // 3  (same idea)
console.log($li.get(0).textContent); // Apple
Try It Yourself

How It Works

  1. toArray. Always means “whole set as Array”.
  2. get() no args. Same whole-set array idea.
  3. get(0). One DOM element — not an array.
  4. Pick by intent. Array of all → toArray; one by index → get(i).

Example 5 — Empty Selection

No matches still return a real empty Array.

jQuery
var arr = $("#does-not-exist").toArray();

console.log(arr.length);         // 0
console.log(Array.isArray(arr)); // true
Try It Yourself

How It Works

  1. Miss. The selector matches nothing.
  2. toArray. Still returns an Array — length 0.
  3. Safe. You can call Array methods without a null check.
  4. Rule. Empty jQuery set → empty native array.

Edge Cases & Pitfalls

Check these before mixing jQuery sets and Arrays.

type

Nodes, not jQuery objects

arr[0].text() fails — use arr[0].textContent or $(arr[0]).text().

live

References, not clones

Changing a node in the array updates the live DOM. Use .clone() if you need copies.

mutate

reverse() mutates the array

Array reverse changes that array in place. It does not reorder the original jQuery set.

get

Do not confuse .get(i) with an array

.get(0) is one element. .toArray() / .get() is the full list.

Browser Support

.toArray() needs jQuery 1.4+ loaded. It works in every modern browser that can run jQuery.

✓ jQuery 1.4+

.toArray()

Works in Chrome, Firefox, Safari, and Edge when jQuery 1.4+ is loaded. Returns a standard Array of matched DOM elements.

100% With jQuery 1.4+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.toArray() Universal

Bottom line: Safe for all supported jQuery versions. Prefer it when you need native Array methods on a selection.

Performance Notes

ApproachCostBest for
.toArray()O(n) copy of referencesNeed a real Array once
Stay on jQuery setNo extra arrayjQuery-only chaining
.get(i)O(1)Single element by index

Cost is proportional to the number of matched elements. For huge sets, convert only when you need Array APIs — otherwise keep chaining on the jQuery object.

Key Takeaways

  • Role: jQuery set → native Array of DOM elements.
  • Why: unlock reverse, map, filter, and friends.
  • vs get: whole array vs one element by index.
  • Empty: no matches → [].

One line: .toArray() hands you a real Array of the matched DOM nodes.

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()

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

toggle() tutorial →

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