javaScript Remove Whitespace from Beginning and End of String

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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