Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 231 characters in body
Source Link
Suresh Prajapati
  • 4.6k
  • 6
  • 29
  • 40

var str = " \n this is a string \n \n \n" console.log(str); console.log(str.trim());

String.trim() removes whitespace from the beginning and end of strings... including newlines.

const myString = " \n \n\n Hey! \n I'm a string!!! \n\n"; const trimmedString = myString.trim(); console.log(trimmedString); // outputs: "Hey! \n I'm a string!!!" 

Here's an example fiddle: http://jsfiddle.net/BLs8u/

NOTE! it only trims the beginning and end of the string, not line breaks or whitespace in the middle of the string.

String.trim() removes whitespace from the beginning and end of strings... including newlines.

const myString = " \n \n\n Hey! \n I'm a string!!! \n\n"; const trimmedString = myString.trim(); console.log(trimmedString); // outputs: "Hey! \n I'm a string!!!" 

Here's an example fiddle: http://jsfiddle.net/BLs8u/

NOTE! it only trims the beginning and end of the string, not line breaks or whitespace in the middle of the string.

var str = " \n this is a string \n \n \n" console.log(str); console.log(str.trim());

String.trim() removes whitespace from the beginning and end of strings... including newlines.

const myString = " \n \n\n Hey! \n I'm a string!!! \n\n"; const trimmedString = myString.trim(); console.log(trimmedString); // outputs: "Hey! \n I'm a string!!!" 

Here's an example fiddle: http://jsfiddle.net/BLs8u/

NOTE! it only trims the beginning and end of the string, not line breaks or whitespace in the middle of the string.

Add an example, emphasize the exception
Source Link
RobW
  • 10.7k
  • 7
  • 44
  • 40

String.trim() removes whitespace from the beginning and end of strings... including newlines.

const myString = " \n \n\n Hey! \n I'm a string!!! \n\n"; const trimmedString = myString.trim(); console.log(trimmedString); // outputs: "Hey! \n I'm a string!!!" 

Here's an example fiddle: http://jsfiddle.net/BLs8u/

Note it'sNOTE! it only fortrims the beginning and end of the string, not for any line breaks or whitespace in the middle of the string.

String.trim() removes whitespace from the beginning and end of strings... including newlines.

Here's an example fiddle: http://jsfiddle.net/BLs8u/

Note it's only for the beginning and end of the string, not for any line breaks in the middle of the string.

String.trim() removes whitespace from the beginning and end of strings... including newlines.

const myString = " \n \n\n Hey! \n I'm a string!!! \n\n"; const trimmedString = myString.trim(); console.log(trimmedString); // outputs: "Hey! \n I'm a string!!!" 

Here's an example fiddle: http://jsfiddle.net/BLs8u/

NOTE! it only trims the beginning and end of the string, not line breaks or whitespace in the middle of the string.

added 112 characters in body
Source Link
RobW
  • 10.7k
  • 7
  • 44
  • 40

String.trim() removes whitespace from the beginning and end of strings... including newlines.

Here's an example fiddle: http://jsfiddle.net/BLs8u/

Note it's only for the beginning and end of the string, not for any line breaks in the middle of the string.

String.trim() removes whitespace from the beginning and end of strings... including newlines.

Here's an example fiddle: http://jsfiddle.net/BLs8u/

String.trim() removes whitespace from the beginning and end of strings... including newlines.

Here's an example fiddle: http://jsfiddle.net/BLs8u/

Note it's only for the beginning and end of the string, not for any line breaks in the middle of the string.

Proof
Source Link
RobW
  • 10.7k
  • 7
  • 44
  • 40
Loading
Source Link
RobW
  • 10.7k
  • 7
  • 44
  • 40
Loading