js remove splaces code example Example 1: javascript remove all spaces from string str = str.replace(/\s/g, ''); Example 2: js remove spaces str = str.trim();