What is NodeList? It’s confusing but that’s okay!
You aren’t alone so let’s work on it together!
How's everyone? I hope you are doing well!
Today, let's talk about NodeList.
I know it is confusing and I have also looked up a lot of sources to understand the concept, many many times. You can do it!
From W3C schools,
A node list is not an array!
A node list may look like an array, but it is not.
You can loop through the node list and refer to its nodes like an array.
However, you cannot use Array Methods, like valueOf(), push(), pop(), or join() on a node list.
What does this even mean?
Let's check the examples!
(You need to know what DOM, Array, and forEach are to understand the examples)
let arrayPop = ['frank', 'kim', 'crazy'];
arrayPop.pop();
console.log(arrayPop); // (2) ["frank", "kim"]
MDN: The pop() method removes the last element from an array and returns that element.
This method changes the length of the array.
You can still check the length through NodeList!
WOW! You can also loop through them??!! This works exactly like Array then!
Can we still use array method then???
Let's check it out.
Unfortunately, the answer is NO. You can not use Array Method for NodeList. It's just array-like.
I hope you find this useful!
Happy coding!
comment