JS Coding Questions Logo
JS Coding Questions
#349💼 Interview💻 Code

How do you trim a string at the beginning or ending

Advertisement

728x90

The trim method of string prototype is used to trim on both sides of a string. But if you want to trim especially at the beginning or ending of the string then you can use trimStart/trimLeft and trimEnd/trimRight methods. Let's see an example of these methods on a greeting message,

javascript
1var greeting = "   Hello, Goodmorning!   ";
2
3console.log(greeting); // "   Hello, Goodmorning!   "
4
5console.log(greeting.trimStart()); // "Hello, Goodmorning!   "
6
7console.log(greeting.trimLeft()); // "Hello, Goodmorning!   "
8
9console.log(greeting.trimEnd()); // "   Hello, Goodmorning!"
10
11console.log(greeting.trimRight()); // "   Hello, Goodmorning!"

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 6

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
349of476