What's the difference between toArray and makeArray in jQuery ?
Last Updated :
15 Dec, 2022
In this article, we will learn the difference between the toArray() and makeArray() methods in jQuery.
JavaScript toArray() method: This method is used on DOM (Document Object Model) elements to convert them into a JavaScript array. We can iterate this array, calculate its length, and access the elements using their indices like a normal array. However, we cannot create an array from other array-like objects using this method. This is the main difference between the two functions.
Syntax:
let dom_array = $("p").toArray();
Example: In this example, we will see how toArray() can be used to convert DOM elements to an array and how it does not work with other types of data.
HTML
<script src="https://code.jquery.com/jquery-3.6.0.js">
</script>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<p>Data Structures</p>
<p>Competitive Programming</p>
<p>Algorithms</p>
<button>Click Here</button>
<h3>Output</h3>
<div id="output"></div>
<script>
let obj = {
name: "Sam",
age: 12
}
$("button").click(function () {
// Creating an array of paragraphs
let para_array = $("p").toArray();
// Iterating over para_array
// and appending the data in the div
for (i = 0; i < para_array.length; i++) {
$("#output").append("Element: " +
para_array[i].innerHTML + "<br>");
}
// We cannot use toArray() on the object
// as toArray() works only for DOM elements
// let obj_array = jQuery.toArray(obj);
// console.log(obj_array);
});
</script>
Output:
JavaScript makeArray() method: This method is used on array-like objects to converts them to an array. We can then use normal array functions on that array. This method supports the conversion of DOM elements as they are array-like objects.
Syntax:
// Array using DOM elements
let array = jQuery.makeArray($("p"));
// Array using other array-like objects
let array2 = jQuery.makeArray(array1, array2, array3);
Example: In this example, we will see how makeArray() can be used to convert all types of array-like elements including DOM elements.
HTML
<script src=
"https://code.jquery.com/jquery-3.6.0.js">
</script>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<p>Data Structures</p>
<p>Competitive Programming</p>
<p>Algorithms</p>
<button>Click Here</button>
<h3>Output</h3>
<div id="output"></div>
<h3>Output 2</h3>
<div id="output2"></div>
<script>
let obj = {
name: "Sam",
age: 12
}
$("button").click(function () {
// Creating an array of paragraphs
let para_array = jQuery.makeArray($("p"));
// Iterating over para_array
// and appending the data in the first div
for (let i = 0; i < para_array.length; i++) {
$("#output").append("Element: " +
para_array[i].innerHTML + "<br>");
}
// Creating an array of the object
let obj_array = jQuery.makeArray(obj);
// Iterating over obj_array
// and appending the data in the second div
for (let i = 0; i < obj_array.length; i++) {
$("#output2").append("Name: " +
obj_array[i].name + " Age: " +
obj_array[i].age + "<br>");
}
});
</script>
Output:
Difference between toArray() and makeArray():
JavaScript toArray() Method
| JavaScript makeArray() Method
|
---|
This method supports the conversion of DOM elements to an array. | This method supports the conversion of all array-like elements to an array. |
Only DOM elements are supported to be converted. Other array-like elements will throw an error. | All types of elements can be converted to an array including DOM elements. |
It does not take any parameters | It only takes one parameter which is an object. |
Its return value is the elements matched by the jQuery selector as an array. | Its return value is an array. |
Similar Reads
What is the difference between unshift() and Push() method in JavaScript? JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array. Javascript Array unshift() method: The JavaScript Array unshift() Method
2 min read
Difference between forEach() and map() loop in JavaScript The forEach() and map() methods in JavaScript are used to iterate over arrays, but they serve different purposes. forEach() executes a provided function once for each array element without returning a new array, while map() transforms elements and returns a new array.JavaScript forEach() JavaScript'
4 min read
Difference between Object.keys() and Object.entries() methods in JavaScript Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties: Object.keys() returns an array of a given object's own enumerable property names, while Object.entries() returns an array of a g
2 min read
What is the difference between Array.slice() and Array.splice() in JavaScript ? In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements. slice():The slice() method in JavaScript extracts a sec
3 min read
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read