JavaScript | String trim()

str.trim()
str.trim() function is used to remove the white spaces from both the ends of the given string. The syntax of the function is as follows:

str.trim()

Arguments
This function does not take any arguments.

Return value
This function returns a new string, without any of the leading or the trailing white spaces.

Examples for the above function are provided below:

Example 1:



var str = "GeeksforGeeks      ";
var st = str.trim();
print(st);

Output:

GeeksForGeeks

In this example the trim() function removes all the leading and the trailing spaces in the string str.

Example 2:

var str = "   GeeksforGeeks";
var st = str.trim();
print(st);

Output:

GeeksForGeeks

In this example the trim() function removes all the leading and the trailing spaces in the string str.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate trim() function
  
function func() {
  
    // Original string containing whitespace
    var str = "GeeksforGeeks      ";
  
    // Trimmed string
    var st = str.trim();
    document.write(st); 
}
  
func();
</script>

chevron_right


Output:

GeeksForGeeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate trim() function
  
function func() {
  
    // Original string containing whitespace
    var str = "   GeeksforGeeks";
  
    // Trimmed string
    var st = str.trim();
    document.write(st);  
}
func();
</script>

chevron_right


Output:



GeeksForGeeks

str.trimLeft()
str.trimLeft() function is used to remove the white spaces from the start of the given string. It does not affect the trailing white spaces. The syntax of the function is as follows:

str.trimLeft()

Arguments
This function does not take any arguments.

Return value
This function returns a new string, without any of the leading white spaces.

Examples for the above function are provided below:

Example 1:

var str = "      GeeksforGeeks      ";
var st = str.trimLeft();
print(st);

Output:

GeeksForGeeks

In this example the trimLeft() function removes all the leading spaces, but the trailing spaces in the string str remain as they are.

Example 2:

var str = "   GeeksforGeeks";
var st = str.trim();
print(st);

Output:

GeeksForGeeks

In this example the trimLeft() function removes all the leading spaces from str.



Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate trimLeft()
// funtion
  
function func() {
  
    // Original string containing whitespace
    var str = "GeeksforGeeks      ";
  
    // Trimmed string
    var st = str.trimLeft();
    document.write(st);  
}
  
func();
</script>

chevron_right


Output:

GeeksForGeeks       

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate trimLeft()
// function
  
function func() {
  
    // Original string containing whitespace
    var str = "   GeeksforGeeks";
  
    // Trimmed string
    var st = str.trimLeft();
    document.write(st);  
}
func();
</script>

chevron_right


Output:

GeeksForGeeks

str.trimRight()
str.trimRight() function is used to remove the white spaces from the end of the given string. It does not affect the white spaces at the start of the string.The syntax of the function is as follows:

str.trimRight()

Arguments
This function does not take any arguments.

Return value
This function returns a new string, without any of the trailing white spaces.

Examples for the above function are provided below:

Example 1:



var str = "GeeksforGeeks      ";
var st = str.trimRight();
print(st);

Output:

GeeksForGeeks

In this example the trimRight() function removes all the trailing spaces from the string str.

Example 2:

var str = "   GeeksforGeeks";
var st = str.trimRight();
print(st);

Output:

     GeeksForGeeks

In this example the trimRight() function does not remove the leading spaces from str.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript Program to illustrate trimRight()
// function
<script>
function func() {
  
    // Original string containing whitespace
    var str = "GeeksforGeeks      ";
  
    // Trimmed string
    var st = str.trimRight();
    document.write(st);  
}
func();
</script>

chevron_right


Output:

GeeksForGeeks       

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate trimRight() 
// funtion
  
function func() {
  
    // Original string containing whitespace
    var str = "   GeeksforGeeks";
  
    // Trimmed string
    var st = str.trimRight();
    document.write(st);  
}
  
func();
</script>

chevron_right


Output:

     GeeksForGeeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.