let a = "How to search and replace a char in a string"; while (a.search(" ", "")) { a = a.replace(" ", ""); } console.log(a);