Conditional Matches

querySelectorAll gives an empty nodeList when no matching elements are found. Using forEach on that empty nodeList doesn’t result in any error either.

When forEach is run on an empty nodeList or an empty array, it just does nothing because it has nothing to do it on.

So, no matching elements is a safe situation that occurs with no error.

1 Like