Title Case a Sentence (forEach vs map)

Tell us what’s happening:
If i use forEach() loop here. The code gives a error. What forEach loop doing here? How to figure it out? if i use map function it gives correct results. According to me forEach doesn’t return anything while map function creates a new array.

Your code so far

    const Words = str.toLowerCase().split(" ").forEach(word=> {
        return word[0].toUpperCase()+word.slice(1);
    return Words.join(" ");
  titleCase("I'm a little tea pot");

The problem is that forEach doesn’t return anything. It just does what you want on each thing. It returns undefined. So, that is what Words is getting set to. Then you try to do a join which is not a prototype method for type undefined. The prototype method map does what you’re trying to do here.