Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to convert a DOM nodelist to an array using JavaScript ?

  • Last Updated : 04 Oct, 2021

NodeList objects are the collection of nodes, usually returned by properties such as Node.childNodes and methods such as document.querySelectorAll(). Although NodeList is not an actual Array but it is possible to iterate over it with the help of forEach() method. NodeList can also be converted into actual array by following methods.

Method 1: By using Array.prototype.slice.call() method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Syntax: 
const array = Array.prototype.slice.call(nodeList);
  • Example: 

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
 
<body>
     
<p>Hello geeks 1</p>
 
     
<p>Hello geeks 2</p>
 
     
<p>Hello geeks 3</p>
 
     
<p>Hello geeks 4</p>
 
 
    <script>
 
        // This is nodeList
        const nodeList = document.querySelectorAll('p');
        console.log(nodeList);
 
        // Converting using Array.prototype.slice.call
        const array1 = Array.prototype.slice.call(nodeList);
        console.log(array1);
    </script>
</body>
 
</html>
  • Output:

Method 2: With the help of Array.from() method.



  • Syntax: 
const array = Array.from(nodeList);
  • Example: 

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
 
<body>
     
<p>Hello geeks 1</p>
 
     
<p>Hello geeks 2</p>
 
     
<p>Hello geeks 3</p>
 
     
<p>Hello geeks 4</p>
 
 
    <script>
 
        // This is nodeList
        const nodeList = document.querySelectorAll('p');
        console.log(nodeList);
 
        // Converting to array using Array.from() method
        const array = Array.from(nodeList);
        console.log(array);
    </script>
</body>
 
</html>
  • Output:

Method 3: Using ES6 spread syntax, it allows an iterable such as an array expression or string to be expanded in places where zero or more arguments or elements are expected. 

  • Syntax: 
const array = [ ...nodeList ]
  • Example: 

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
 
<body>
     
<p>Hello geeks 1</p>
 
     
<p>Hello geeks 2</p>
 
     
<p>Hello geeks 3</p>
 
     
<p>Hello geeks 4</p>
 
 
    <script>
 
        // This is nodeList
        const nodeList = document.querySelectorAll('p');
        console.log(nodeList);
 
        // Converting using Spread syntax
        const array1 = [...nodeList];
        console.log(array1);
    </script>
</body>
 
</html>
  • Output:

Note: Method 2 and Method 3 may not work in older browsers properly but will work fine in all modern browsers.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!