const theName = "Vincent van Gogh"; function hideWord(word) { if (word.length < 2) return word; return word.substring(0, 1) + '*'.repeat(word.length-1); } console.log(theName.split(" ").map(hideWord).join(" ")); OUTPUT: V****** v** G***
You could also wrap the whole lot in a function if you wanted to be able to call it from multiple places and avoid redefining it.
function hideWords(allTheWords) { return allTheWords.split(" ").map(hideWord).join(" "); } console.log(hideWords(theName));