3 ms·
Please, using for loops is okay, especially if you're not actually writing functional code. [...document.xpath("//text()[contains(., 'Hello')]")] .f
by gridlockd 6y ago
Please, using for loops is okay, especially if you're not actually writing functional code.
[...document.xpath("//text()[contains(., 'Hello')]")]
.forEach(node => node.textContent = node.textContent.replace("Hello", "Greetings"));
versus
for (const node of document.xpath("//text()[contains(., 'Hello')]"){
node.textContent = node.textContent.replace("Hello", "Greetings");
}
No allocation of a pointless array, no redundant iteration, fewer "[]", "()" and no "=>" or "...". No downside whatsoever!
- RussianCow 6y ago> No downside whatsoever! That's not entirely true if you need to support IE, in which case Babel (or whatever compiler you're using) will generate extra code for the `for...of` loop. But even then, I would still recommend using it for the reasons you mentioned.