I have this code:
var str = ' abc'; str.replace(" ",""); but it is not working.
First, you have spelling error:
replce You should do:
var str = ' abc'; str = str.replace(/ /g,""); The /g is global modifier which will replace the specified text throughout the given string.
Alternatively, you can use split and join like this:
var str = ' abc'; str = str.split(" ").join(""); Try this in any browser url
javascript:alert('strin g'.replace(/ /g, '')); You need to use regex, not plain strings for js string.replace
var str = ' abc'; str = str.replace(/ /g, ''); String.replace returns the modified string, it doesn’t change the string in-place.