VOOZH about

URL: https://www.geeksforgeeks.org/javascript/javascript-string-trim-method/

⇱ JavaScript String trim() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JavaScript String trim() Method

Last Updated : 16 Jan, 2026

The trim() method is used to remove extra spaces from a string. It helps clean user input by removing unwanted whitespace.

  • Removes whitespace from both ends of the string.
  • Returns a new trimmed string.
  • The original string is not modified.

Syntax

str.trim();

Parameters

This method does not accept any parameter.

Return value

Returns a new string without any of the leading or trailing white spaces. 

[Example 1]: Trimming Whitespace from the End of a String.

[Example 2]: Trimming Leading Whitespace from a String


Output
GeeksforGeeks

[Example 3]: Trimming Whitespace from both sides of String

Comment