How to Remove Line Breaks from the Start and End of a String in JavaScript?

Estimated read time 1 min read

To remove line breaks from the start and end of a string in JavaScript, you can use the trim() method. Here’s an example:

let str = " \n\r This is a string \n\r ";
let newStr = str.trim();
console.log(newStr);

This code removes line breaks from the start and end of the string " \n\r This is a string \n\r " using the trim() method, and logs the modified string: "This is a string".

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply