As we know there is a .forEach()
method for arrays in JavaScript. But Strings don't have that method built in.
So, is there any issue regarding the following code snippet:
String.prototype.forEach = Array.prototype.forEach
?
This setting helps me to use .forEach
for Strings.
let myName = "Avetik";
String.prototype.forEach = Array.prototype.forEach;
myName.forEach(char => {
console.log(char);
})
The above code works fine and outputs all chars of my name.
You already guessed that I am a newbie in JS.