
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Trim a String at Beginning or Ending in JavaScript
While working with the data, removing the unnecessary white spaces from the string is necessary. So, we need to trim a string at the beginning or end.
If we keep unnecessary white spaces in the data, it can cause some issues. For example, while storing the password, if we don't trim the white space, it can mismatch when users try to log in another time to the app.
In this tutorial, we will learn to trim a string at beginning or ending in JavaScript.
Use the trimRight() method to trim the end of the string
The trimRight() method allows us to remove the whitespaces from the end of the string.
Syntax
Users can follow the syntax below to use the trimRight() method to trim a string from the end.
let result = string1.trimRight();
In the above syntax, string1 is a string to trim from the end, and we store the final string in the result variable.
Example 1
In the example below, we have created two strings that contain the whitespaces at the start and end of the string. After that, we used the trimRight() method to remove the white spaces from the end of the string.
<html> <body> <h2>Using the <i> trimRight() </i> method to trim the string from the end in JavaScript.</h2> <div id = "output"></div> <br> </body> <script> let output = document.getElementById("output"); let string1 = " Trim from right! "; let string2 = "Trim from the end ! "; string1 = string1.trimRight(); string2 = string2.trimRight(); output.innerHTML += "The final string1 is *" + string1 + "*. <br>"; output.innerHTML += "The final string2 is *" + string2 + "*. <br>"; </script> </html>
Use the trimLeft() method to trim the start of the string
We can trim the string from the start using the trimLeft() method.
Syntax
Users can follow the syntax below to use the trimLeft() method to remove white spaces from the start of the string.
let pass1 = pass1.trimLeft();
In the above syntax, we have used the trimLeft() method with the pass1 string.
Example 2
In the example below, we have two password strings containing the white spaces at the start. Afterwards, we used the trimLeft() method to remove the white spaces from the start of the string.
Users can observe the strings without white spaces at the beginning of the output.
<html> <body> <h2>Using the <i> trimLeft() </i> method to trim the string from the start in JavaScript.</h2> <div id = "output"></div> <br> </body> <script> let output = document.getElementById("output"); let pass1 = " abcd@123 " let pass2 = " pok.=-E3434"; pass1 = pass1.trimLeft(); pass2 = pass2.trimLeft(); output.innerHTML += "The final string1 is *" + pass1 + "*. <br>"; output.innerHTML += "The final string2 is *" + pass2 + "*. <br>"; </script> </html>
Use the trim() method to trim the left end and right end of the string together
The trim() method of the string library allows us to remove all white spaces from the start and end of the string in a single shot rather than use the trimLeft() and trimRight() methods separately.
Syntax
Users can follow the syntax below to trim a string at the beginning or end using the trim() method.
str = str.trim();
In the above syntax, str is a string containing whitespaces at the string's start and end.
Example 3
In the example below, we allow users to enter the string with white spaces in the prompt box. After that, we used the trim() method to remove white spaces and shown the final result in the output.
<html> <body> <h2>Using the <i> trim </i> method to trim the string from the start in JavaScript.</h2> <div id = "output"></div> <br> </body> <script> let output = document.getElementById("output"); let str = prompt("Enter the string with white spaces at start and end.", " abcd efg ") str = str.trim(); output.innerHTML += "The final string1 is *" + str + "*. <br>"; </script> </html>
We learned to trim a string from beginning and end using various methods. The trimLeft() method is used to trim a string from the left, and the trimRight() method is to trim the string from the right. Also, we used the trim() method to trim white spaces from the start and end.
Also, users can use the trimStart() and trimEnd() methods to trim the strings from both ends.