/  Technology   /  JavaScript String trim()

JavaScript String trim()

 

The trim() function is a built-in string function in JavaScript that is used to trim strings. Using this function, the whitespace is removed from both ends, i.e., the start and the end of the string. Since trim() is a string method, it is invoked by an instance of the String class. To use the trim() method, we must create an instance of the String class.

Syntax

str.trim()  

In this case, str is a String class of objects that will contain the string to be trimmed.

Parameters

There are no arguments or parameters in the trim() method.

Return Value

String.trim() returns the string after removing whitespace characters from the beginning and end of the string.

Examples

The following examples demonstrate how to remove elements from an array using the trim() function. These examples demonstrate how to use this JavaScript function. Here are a few examples

Example

We will pass a string containing whitespace at both ends in this example.

<html>
<body>
<script>
function func_trim() {
//original string with whitespace in beginning and end
var str = " javatpoint i2Tutorials website ";
//string trimmed using trim()
var trimmedstr = str.trim();
document.write(trimmedstr);
}
func_trim();
</script>
</html>
</body>

 

Leave a comment