javaScript Remove Whitespace from Beginning and End of String

javascript remove whitespace from string; In this tutorial, you’ll learn how to javascript remove whitespace from beginning and end of string using JavaScript trim() method.

JavaScript trim() method

The javascript String.trim()is a string method that is used to remove whitespace characters from the beginning and from the end of a string.

let res = str.trim();

Here,

  • The trim() method doesn’t change the original string.

if you want to remove only starting whitespace or ending whitespace from the given string, You can use javascript trimStart() or trimEnd() methods. See the followings:

In JavaScripttrimStart() is a string method that is used to remove whitespace characters from the start of a string.

In JavaScripttrimEnd() is a string method that is used to remove whitespace characters from the end of a string.

Note that, The whitespace characters include space, tab, no-break space, etc.

Examples: JavaScript trim()

Let’s take a took the following example:

Use the trim() to remove whitespace from both sides of a given string, see the following example:

let str = '  javascript programming  ';
let result = str.trim();

console.log(result);

Output:

"javascript programming"

Conclusion

In this tutorial, you have learned how to remove whitespace characters from both ends of a string using the js trim() method.

Recommended JavaScript Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *