Skip to content
Related Articles

Related Articles

Improve Article

JavaScript String trim() Method

  • Difficulty Level : Easy
  • Last Updated : 07 Oct, 2021
Geek Week

Below is the example of the String trim() Method. 

  • Example:

JavaScript




<script>
function func() {
    var str = "GeeksforGeeks      ";
    var st = str.trim();
    document.write(st);
}
func();
</script>
  • Output:
GeeksForGeeks

str.trim() method is used to remove the white spaces from both the ends of the given string.

Syntax:  

str.trim()

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

Examples for the above method are provided below:

Example 1: 



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

Output:  

GeeksForGeeks

In this example the trim() method 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() method removes all the leading and the trailing spaces in the string str.

Codes for the above method are provided below:

Program 1:  

JavaScript




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

Output: 



GeeksForGeeks

Program 2:  

JavaScript




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

Output: 

GeeksForGeeks

str.trimLeft() 
str.trimLeft() method is used to remove the white spaces from the start of the given string. It does not affect the trailing white spaces. 

Syntax:  

str.trimLeft()

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

Examples for the above method are provided below:

Example 1:  

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

Output:  

GeeksForGeeks

In this example the trimLeft() method 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() method removes all the leading spaces from str.

Codes for the above method are provided below:

Program 1:  

JavaScript




<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>

Output: 

GeeksForGeeks       

Program 2: 

JavaScript




<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>

Output: 

GeeksForGeeks

str.trimRight() 
str.trimRight() method 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.

Syntax: 



str.trimRight()

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

Examples for the above method are provided below:

Example 1:  

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

Output:  

GeeksForGeeks

In this example the trimRight() method 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() method does not remove the leading spaces from str.

Codes for the above method are provided below:

Program 1:  

JavaScript




// 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>

Output: 

GeeksForGeeks       

Program 2: 

JavaScript




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

Output: 

     GeeksForGeeks

 

Supported Browser:

  • Chrome 4 and above
  • Edge 12 and above
  • Firefox 3.5 and above
  • Internet Explorer 10 and above
  • Opera 10.5 and above
  • Safari 5 and above

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :