JS Built-ins
← String178 / 195
StringString.prototype.trimEndbeginner

String.prototype.trimEnd() β€” both ends β†’ only leading preserved

πŸ“– Usage Example
'  hello  '.trimEnd()   // β†’ '  hello'
'text   '.trimEnd()    // β†’ 'text'
'hi'.trimEnd()         // β†’ 'hi'

Use trimEnd() to remove only trailing whitespace from the end of a string, leaving leading whitespace intact.

String.prototype.trimEnd() β€” Only Trailing Removed

When both leading and trailing whitespace exist, trimEnd() leaves the leading intact.

Challenge: Implement keepLeading(str) using trimEnd.

keepLeading('   hello   ') // β†’ '   hello'
πŸ’‘ Hints (1)
  • β€’ Compare `trim()` (both ends) vs `trimEnd()` (right end only) vs `trimStart()` (left end only).