Start learning free

LESSON 69 OF 671 · STRINGS I

String Strip / Trim in JavaScript

Lesson

In JavaScript, 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.

STEP 1 OF 1
    Ctrl + Enter to check