jQuery .toArray() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Collection API

What You’ll Learn

The .toArray() method turns a jQuery collection into a plain JavaScript Array of DOM elements. This tutorial covers the official demos, using native Array methods like .reverse() and .map(), comparisons with .get(), empty selections, five examples, and try-it labs.

01

.toArray()

Native Array

02

DOM nodes

Not jQuery

03

.reverse()

Array methods

04

vs .get()

Similar API

05

Empty

Returns []

06

Since 1.4

Stable API

Introduction

A jQuery object is array-like — it has a .length and numeric indexes — but it is not a real JavaScript Array. That means some Array methods are missing or behave differently.

Since jQuery 1.4, .toArray() bridges that gap: it returns every matched DOM element in a standard array so you can call .reverse(), .map(), .filter(), for…of, or hand the list to another library.

The nodes are the same live elements in the document (not clones). Pair this topic with collection helpers like .size() / .length, and browse the jQuery DOM hub for related methods.

Understanding the .toArray() Method

.toArray() copies the matched set into a new Array instance. Each entry is a raw DOM element. Order matches the jQuery collection (typically document order).

After conversion you leave the jQuery chaining world. To call jQuery methods again on one item, wrap it: $(nodes[0]).addClass("x"). Or wrap the whole array: $(nodes).

💡
Beginner Tip

Think: $("li") = jQuery toolbox. $("li").toArray() = plain box of DOM elements you can sort, reverse, or map with normal JavaScript.

📝 Syntax

Signature (jQuery 1.4+):

jQuery
.toArray()

Parameters

  • None — this method does not accept any arguments.

Return value

  • Array — all elements in the jQuery set as native DOM nodes.

Typical pattern

jQuery
var nodes = $( "li" ).toArray();
console.log( nodes.length );
console.log( nodes[0].tagName ); // "LI"

⚡ Quick Reference

GoalCode
Get a native array$("div").toArray()
Reverse then use$("div").toArray().reverse()
Map to text$("li").toArray().map(el => el.textContent)
Same via .get()$("div").get() (no index)
Empty selection$("#none").toArray() // []
.toArray()
Array

All matched DOM elements in a real Array

.get()
array | el

No args ≈ toArray; with index → one element

Contents
DOM

Raw nodes — wrap with $() to use jQuery again

Empty
[]

Safe empty array when nothing matched

📋 .toArray() vs .get() vs jQuery object

jQuery object.toArray().get() / .get(i)
TypejQueryArrayArray or Element
Chain jQuery methodsYesNo (until re-wrapped)No
Native Array methodsLimitedFullFull (when no index)
Best forDOM opsArray pipelinesIndex access or dump

Examples Gallery

Examples follow the official jQuery API and everyday Array workflows. Use View Output or Try It Yourself to explore each case.

📚 Getting Started

Official-style demos for converting the matched set to an Array.

Example 1 — Official Pattern: Dump List Items

Call .toArray() on every <li> to get a native array of those DOM nodes.

jQuery
alert( $( "li" ).toArray() );
Try It Yourself

How It Works

$("li") builds the jQuery set. .toArray() returns those same elements as a standard array. Alerting an array of elements shows the browser’s default object string for each node.

Example 2 — Official Pattern: Reverse Div Contents

Convert all <div> elements to an array, reverse it, then join their innerHTML for display.

jQuery
function disp( divs ) {
  var a = [];
  for ( var i = 0; i < divs.length; i++ ) {
    a.push( divs[ i ].innerHTML );
  }
  $( "span" ).text( a.join( " " ) );
}

disp( $( "div" ).toArray().reverse() );
Try It Yourself

How It Works

jQuery collections are not full Arrays, so chaining .reverse() on $("div") is not the right tool. After .toArray(), Array.prototype methods apply. The demo only reverses the array order used for reading text — it does not reorder the DOM.

📈 Practical Patterns

Mapping, empty sets, and how .get() compares.

Example 3 — Map Elements to Text With .map()

Once you have a native array, use Array.prototype.map to collect strings.

jQuery
var labels = $( "li" ).toArray().map(function ( el ) {
  return el.textContent.trim();
});
$( "#out" ).text( labels.join( " | " ) );
Try It Yourself

How It Works

.toArray() unlocks the standard Array toolkit. You could also use jQuery’s .map() and .get(), but converting first is often clearer when the rest of the pipeline is plain JavaScript.

Example 4 — Empty Selection Returns []

Missing matches still return a real (empty) array — safe for .length checks and spreads.

jQuery
var nodes = $( "#does-not-exist" ).toArray();
console.log( Array.isArray( nodes ) ); // true
console.log( nodes.length );           // 0
Try It Yourself

How It Works

Like other jQuery collection methods, an empty match is not an error. You get [], so downstream code can always assume an array.

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

.get() with no arguments returns the same kind of array; .get(0) returns one element.

jQuery
var a = $( "li" ).toArray();
var b = $( "li" ).get();
var first = $( "li" ).get( 0 );

console.log( a.length === b.length ); // true
console.log( first.tagName );         // "LI"
Try It Yourself

How It Works

Both dump the collection to an array when called without an index. Choose .toArray() when readability matters; use .get(i) (or $("li")[i]) when you only need one element.

🚀 Common Use Cases

  • Array pipelines — reverse, sort, map, or filter matched elements with native methods.
  • Interop — pass DOM nodes to libraries that expect a real Array.
  • Debugging — inspect the matched set as a plain list in the console.
  • Custom loopsfor…of or indexed for over DOM elements.
  • Partial extraction — convert, then take a slice of nodes for a batch update.
  • Readable intent — prefer .toArray() over .get() when the goal is clearly “become an Array.”

🧠 How .toArray() Converts the Set

1

Match with jQuery

Select elements into a jQuery collection.

Select
2

Call .toArray()

Build a new Array holding references to those DOM nodes.

Convert
3

Use Array APIs

Reverse, map, filter, or iterate with plain JavaScript.

Array
4

Optional: wrap again

$(nodes) or $(nodes[i]) to return to jQuery.

📝 Notes

  • Available since jQuery 1.4; still present in modern jQuery 3.x / 4.x.
  • Returns an Array of DOM elements — not jQuery objects.
  • Does not clone nodes; array entries are live element references.
  • Empty selections return [].
  • .get() with no arguments is effectively the same dump to an array.
  • Reversing the array does not reorder the DOM unless you move nodes yourself.
  • Modern alternative: Array.from($("li")) or [...$("li")] in environments that support them — .toArray() remains the clearest jQuery API.

Browser Support

.toArray() has been part of jQuery since 1.4+. It works in every browser supported by your jQuery build — including modern evergreen browsers with jQuery 3.x and 4.x.

jQuery 1.4+

jQuery .toArray()

Stable collection-to-Array bridge across jQuery 1.4–4.x. No browser polyfills required — jQuery normalizes the conversion.

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

Bottom line: Use it whenever you need native Array methods on matched DOM elements.

Conclusion

.toArray() is the straightforward way to leave jQuery’s collection type and work with a real JavaScript array of DOM elements. Reach for it when Array methods, loops, or third-party APIs expect a native list.

Next up: flipping visibility with .toggle().

💡 Best Practices

✅ Do

  • Use .toArray() before native .map / .filter / .reverse
  • Prefer it over .get() when intent is “give me an Array”
  • Re-wrap with $() if you need jQuery methods again
  • Remember empty selections return [], not null
  • Keep DOM mutations explicit — reversing an array alone does not move nodes

❌ Don’t

  • Expect array entries to be jQuery objects
  • Assume .toArray() clones elements
  • Call jQuery chain methods on the returned array without wrapping
  • Confuse array order changes with visual DOM order changes
  • Convert to an array when a simple jQuery .each() would suffice

Key Takeaways

Knowledge Unlocked

Five things to remember about .toArray()

The bridge from jQuery collections to native Arrays.

5
Core concepts
DOM 02

Holds

Elements

Live refs
03

Like

.get()

No index
04

Empty

[]

Safe
1.4 05

Since

jQuery 1.4

API

❓ Frequently Asked Questions

.toArray() returns a standard JavaScript Array containing all DOM elements in the current jQuery set, in document order. It takes no arguments. Available since jQuery 1.4.
Calling .get() with no arguments also returns an array of all matched elements — the same idea as .toArray(). .get(index) returns a single DOM element (or undefined). Prefer .toArray() when you want a clear “give me a native array” intent; use .get(n) for one element by index.
Plain DOM Element nodes (HTMLElement, etc.), not jQuery wrappers. To use jQuery methods on one of them again, wrap it: $(array[0]).
So you can use Array methods that jQuery collections do not expose the same way — for example .reverse(), .map(), .filter(), .find(), for…of, or pass the list into APIs that expect a real Array.
An empty array: []. For example $("#missing").toArray() is [].
It builds a new Array that holds references to the same DOM nodes already in the page (or in memory). It does not clone elements. Mutating a node in the array mutates the live DOM element.
Did you know?

.toArray() arrived in jQuery 1.4 alongside other collection refinements. Before that, developers often relied on .get() with no arguments or manual loops pushing this into an array inside .each(). The dedicated method made the “I need a real Array” intent obvious in reading and reviewing code.

Next: jQuery .toggle() Method

Flip element visibility instantly or with a short animation.

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

6 people found this page helpful