Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of data does querySelectorAll return? [duplicate]

A javaScript object does not have a length property, but the returned value of querySelectorAll has a length property, indicating that it's an array. But if we check it by Array.isArray() then it returns false, proving that it is not an array. What type of data is it then?

var obj1 = {
  fname: "Mirajul",
  lname: "Momin",
  age: 24
};
console.log(obj1.length);
var paraList = document.querySelectorAll("p");
console.log(paraList.length);
console.log(Array.isArray(paraList));
<p>This is paragraph one</p>
<p>This is paragraph two</p>
<p>This is paragraph three</p>
<p>This is paragraph four</p>
like image 980
Mirajul Momin Avatar asked Mar 25 '26 05:03

Mirajul Momin


1 Answers

The Element method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.

For the differences please visit: Difference between HTMLCollection, NodeLists, and arrays of objects

You can use Spread syntax to make that as an array:

var obj1 = {
  fname: "Mirajul",
  lname: "Momin",
  age: 24
};
console.log(obj1.length);
var paraList = [...document.querySelectorAll("p")];
console.log(paraList.length);
console.log(Array.isArray(paraList));
<p>This is paragraph one</p>
<p>This is paragraph two</p>
<p>This is paragraph three</p>
<p>This is paragraph four</p>
like image 174
Mamun Avatar answered Mar 27 '26 17:03

Mamun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!