Example 1
Remove spaces with trim[]:
let text = " Hello World! ";
let result = text.trim[];
Try it Yourself »
Remove spaces with replace[] using a regular expression:
let text = " Hello World!
";
let result = text.replace[/^\s+|\s+$/gm,''];
Try it Yourself »
Definition and Usage
The trim[]
method removes whitespace from both sides of a string.
The trim[]
method does not change the original string.
Syntax
Parameters
Return Value
Type | Description |
A string | A string with removed whitespace from both ends. |
Browser Support
trim[]
is an ECMAScript5 [ES5] feature.
ES5 [JavaScript 2009] fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
The A new string representing If neither the beginning or end of To return a new string with whitespace
trimmed from just one end, use The following example displays the lowercase string trim[]
method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters [space, tab, no-break space, etc.] and all the line terminator characters [LF, CR, etc.]. Try it
Syntax
Return value
str
stripped of whitespace from both its beginning and end.str
has any whitespace, a new string is still returned [essentially a copy of str
], with no exception being thrown.trimStart[]
or trimEnd[]
.Examples
Using
trim[]
'foo'
:const orig = ' foo ';
console.log[orig.trim[]]; // 'foo'
Specifications
ECMAScript Language Specification # sec-string.prototype.trim |
Browser compatibility
BCD tables only load in the browser
See also
The trim[] is a built-in string function in JavaScript, which is used to trim a string. This function removes the whitespace from both the ends, i.e., start and end of the string. As the trim[] is a string method, so it is invoked by an instance of the String class. We have to create an instance of String class with which the trim[] method will be used.
Note - The trim[] method does not change the original string; it just removes the leading and trailing whitespace character.
Syntax
The syntax of trim[] method is as follows:
Here, str is a String class of object that will contain the string to be trimmed.
Parameters
The trim[] method does not have any arguments or parameters inside it.
Return Value
The string.trim[] function returns the string after removing whitespace character from that string from the beginning and end of the string.
Examples
Below are some examples, which are using trim[] function to remove the elements from it. In these examples, you will see how to use this JavaScript function. So, here are a few examples -
Example 1
In this example, we will pass a string containing whitespace at both ends.
Below is the example of the String trim[] Method.
- Example:
JavaScript
function
func[] {
var
str =
"GeeksforGeeks "
;
var
st = str.trim[];
document.write[st];
}
func[];
- 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
function
func[] {
var
str =
"GeeksforGeeks "
;
var
st = str.trim[];
document.write[st];
}
func[];
Output:
GeeksForGeeks
Program 2:
JavaScript
function
func[] {
var
str =
" GeeksforGeeks"
;
var
st = str.trim[];
document.write[st];
}
func[];
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
function
func[] {
var
str =
"GeeksforGeeks "
;
var
st = str.trimLeft[];
document.write[st];
}
func[];
Output:
GeeksForGeeks
Program 2:
JavaScript
function
func[] {
var
str =
" GeeksforGeeks"
;
var
st = str.trimLeft[];
document.write[st];
}
func[];
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
function
func[] {
var
str =
"GeeksforGeeks "
;
var
st = str.trimRight[];
document.write[st];
}
func[];
Output:
GeeksForGeeks
Program 2:
JavaScript
function
func[] {
var
str =
" GeeksforGeeks"
;
var
st = str.trimRight[];
document.write[st];
}
func[];
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