javascript trim function

The JavaScript trim function is used to remove whitespace or blank space from inside any string. The whitespace can be removed from the front, back, and middle of the string. For this, you will have to use the trim function “trim ()”. Let’s know how to use trim in JavaScript.

JavaScript Trim Function

Many times when the trim function does not work in a browser, the whitespace can also be removed with the replace function. Today you will know how to remove whitespace from inside the string with trim function and replace function.

/ ***** Removed whitespace from front of string ***** /

<script> 
  
	function TrimFunction() { 

		var MyString = "	Trim in Javascript"; 
	  
		var result = MyString.trim(); 
		
		document.write(result); 
		
	} 

	TrimFunction();
 
</script>


/ ***** Removed whitespace from behind string ***** /

<script> 
  
	function TrimFunction() { 

		var MyString = "Trim in Javascript		"; 
	  
		var result = MyString.trim(); 
		
		document.write(result); 
		
	} 

	TrimFunction();
 
</script>


/ ***** Removed whitespace from front and back of string ***** /

<script> 
  
	function TrimFunction() { 

		var MyString = "	Trim in Javascript		"; 
	  
		var result = MyString.trim(); 
		
		document.write(result); 
		
	} 

	TrimFunction();
 
</script>

Remove Spacing Between String

If you want to remove whitespace from the middle of your string, you can use Replace Function for that.

<script> 
  
	function TrimFunction() { 

		var MyString = "	Trim in 	Javascript	"; 
	  
		var result = MyString.replace(/\s+/g, " ");
		
		document.write(result); 
		
	} 

	TrimFunction();
 
</script>

Remove Spacing Using Replace Function

But let you know that if you use the method of Replace Function shown above, then you can remove the whitespace from the front, back, and middle of your string. Meaning, it not only removes the middle whitespace but also removes the whitespace from the front and back.

<script> 
  
	function TrimFunction() { 

		var MyString = "	Remove	all 	blank	space	"; 
	  
		var result = MyString.replace(/\s+/g, " ");
		
		document.write(result); 
		
	} 

	TrimFunction();
 
</script>

Create Own JavaScript Trim Function

If you want to create your own JavaScript Trim Function, then for that you will have to follow the example shown below.

<script> 

	function CustomTrim(a) {
		return a.replace(/\s+/g, " ");
	}
  
	function MyFunction() { 

		var MyString = "	Remove		blank	space	"; 
	  
		var result = CustomTrim(MyString);
		
		document.write(result); 
		
	} 

	MyFunction();
 
</script>

JavaScript Trim Using Alert

If you want your trimmed string to appear in the alert, then you will have to use the alert function.

<button onclick="TrimFunction()">Click Me</button>

<script> 
  
	function TrimFunction() { 

		var MyString = "	Remove 		black	space	"; 
	  
		var result = MyString.trim(); 
		
		alert(result); 
		
	} 
 
</script>

Other String Functions

NoFunctionDescription
1replace()It searches a string and replaces it with a new string
2search()It searches a string or word and returns the position
3trim()Removes whitespace from the string
4match() It searches for the letters and returns the matched words
5slice()Select the part (Word) of a string
6substr()Selects the parts of the string
7repeat()It repeats the same string
8toLowerCase()It converts the string to lowercase letters
9toUpperCase()It converts the string to uppercase letters
10indexOf()It searches a string and returns the position

Browser Support

  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari
  • Chrome

Note: All the examples you have seen inside this article have all been made by us and tested thoroughly. So you can use all these without any difficulty.

Related: 10 awesome examples of javascript else if condition