var str = ' blabla bla '; while(str[0]== ' ') str = str.slice(1); while(str[str.length-1]==' ') str = str.slice(0, str.length-1); console.log(str);