In TypeScript, you can remove leading and trailing whitespace from a string using trim():
s.trim() // removes leading and trailing whitespace
s.trimStart() // removes only leading whitespace
s.trimEnd() // removes only trailing whitespace
Here are some examples:
let s = " hello ";
console.log(s.trim()); // "hello"
console.log(s.trimStart()); // "hello "
console.log(s.trimEnd()); // " hello"
trim() returns a new string — the original is not modified.
Assignment
Follow the Coding Tutorial and trim whitespace from s!
Hint
Use s.trim() and assign the result to result.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
