JS Built-ins
← String173 / 195
StringString.prototype.trimbeginner

String.prototype.trim() β€” both ends

πŸ“– Usage Example
'  hello  '.trim()      // β†’ 'hello'
'
 text 	'.trim()   // β†’ 'text'
'no spaces'.trim()   // β†’ 'no spaces'

Use trim() to remove all leading and trailing whitespace characters from a string.

String.prototype.trim() β€” Both Ends

.trim() strips whitespace from both the start and end simultaneously.

Challenge: Implement trimBoth(str) that removes whitespace from both ends.

trimBoth('  hello  ') // β†’ 'hello'
πŸ’‘ Hints (1)
  • β€’ `trim()` never removes whitespace from the middle of a string.