var text = "I love JavaScript despite its constructor syntax"; var words = text.toLowerCase().split(/[\s,.]+/); var count = {}; words.forEach(function (w) { if (count[w]) count[w] += 1; else count[w] = 1; }); print("count[love]: " + count.love); print("count[javascript]: " + count.javascript); print("count[constructor]: " + count.constructor);
Output