Trim String in JavaScript


Trim String in JavaScript – Few years ago there was no predefined function to trim in javascript so people adopted many ways to trim the strings. Some people liked to create own function and some used jQuery. But now there is no need to adopt alternative method to trim the strings in javascript string.trim() method is available to trim the strings. Here in this tutorial we are going to explain this with example and demo.


How to trim String in JavaScript Example

Trim is basically used to remove the white space from the string’s both end ie. starting and end point. You can use string.trim() function to trim the string as below-

Trim String in JavaScript Example:

<script type="text/javascript">
var myString = " abc "; // string with space.
var result = myString.trim();
</script>

Try it »

If you run the above example it will produce output something like this –

Trim String in JavaScript Example

The above function works perfectly for all strings and removes white space from both end.


Advertisements

Add Comment

📖 Read More