JavaScript .trim()

Anonymous contributor's avatar
Anonymous contributor
Published Jun 20, 2021Updated Jun 28, 2023
Contribute to Docs

Removes existing whitespace from both ends of a string.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

string.trim();

Example

Removing all existing whitespace from both ends of a string using .trim() method:

console.log(' Hello World '.trim());
// Output: Hello World

Codebyte Example

The following is a runnable example demonstrating the .trim() method:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours