It is common knowledge that there is a .forEach()
method for arrays in JavaScript, but unfortunately Strings do not have that method integrated.
So, the question arises: is it problematic to use the following code snippet:
String.prototype.forEach = Array.prototype.forEach
?
This setup enables me to utilize .forEach
with Strings.
let myName = "Avetik";
String.prototype.forEach = Array.prototype.forEach;
myName.forEach(char => {
console.log(char);
})
The above code functions as expected and displays all characters of my name.
By now, you might have figured out that I am relatively new to JavaScript.