JavaScript Remove Spaces
In this section, yow will learn how to remove spaces from the string.
To remove the spaces, we have simply used JavaScript methods split() and join(). Now to use these methods, we have defined a string. Firstly the split(" ") method splits the string into an array of strings. It takes the string argument, therefore the method enclosed a pair of double quotes which allow the string to be split. Then the method join("") join all the elements of an array into a string. As this method contains a parameter 'separator' which adds all array elements by placing the separator between them. In our example, to make the original string spaces less, first we split the original string by spaces using split(' ') and then join all elements with no space string using join('').
Here is the code:
| <html> <h2>JavaScript Remove Spaces</h2> <script language="javascript" type="text/javascript"> var string="All glitters are not gold"; var newString=string.split(' ').join(''); document.write("The original string is = "+string+"<br>"); document.write("After removing the spaces, the string becomes = "+newString); </script> </html> |
Output will be displayed as:
Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


