Is an array really needed to loop through all elements with a certain textContent?

const [...atLeastOneElement] = document.getElementsByTagName("*");
atLeastOneElement.forEach((element) => {
    if (element.textContent == "someTextContent") {
        element.style.display = 'none';
    }
});

I understand this code as “create a variable which is an array named atLeastOneElement and assign to it all elements whatsoever. Then, for each element in the array atLeastOneElement, change textContent.”

But must we use this somewhat “sugary” or “unorthodox” syntax? Is there something more “readable” for the general audience?

Don’t know what you mean with that?

For me this is super readable. But if you want to make it more old-school you can use a standard for loop as well


const elements = document.getElementsByTagName("*");
for(const element of elements)
{
    if (element.textContent == "someTextContent") {
        element.style.display = 'none';
    }
});

I meant that I don’t think this type of syntax [...SOMETHING] was available before year 2015 when big changes came to JavaScript :slight_smile:

If I understood you correctly than what I had there is basically a case of forEach() method, acting like a for...of loop.

There is a language created more than half a century ago called COBOL that is designed to be easy for non-programmers to read. Programmers do not like it because it is very wordy. Programmers prefer cryptic and compact syntax. COBOL is wordy and C is cryptic. The C language won.

I am a small scale programmer or a humble scripter Who likes the right balance between SOME_HUMAN_LANGUAGE and Mathematics.

What is the right balance? Well, for me it’s “full JavaScript” :slight_smile:

I never worked with COBOL, the main things I have learned about it is that it is very human-readable and primarily used by banking companies.

In my opinion JavaScript is not designed well and any discussion of that could be found or should be continued elsewhere.

COBOL is not a realistic choice, especially for websites. I think an improved version of it would be great and many programmers would like it but it is not supported as well as it could be. As for banking companies, COBOL is the acronym for COmmon Business-Oriented Language.

What is relevant here is that programming languages tend to be cryptic.

Yes this is because getElementsByTagName() returns a HTML collection, which doesn’t have the forEach() method we’d like to use; so we convert it to an array. However you might also use querySelectorAll() instead, which returns a node list, which again does implement forEach() and doesn’t require conversion:

const elements = document.querySelectorAll('*')

elements.forEach(element => {
  // ...
})

BTW another point to note is that unlike a node list, a HTML collection is live. This is rarely what you want, and converting it to an array has the additional effect of making the results static.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.