remove extra space in string js

JavaScript
newString = string.replace(/\s+/g,' ').trim();
Source

Also in JavaScript: