trim, padStart, padEnd

Trimming and padding strings.

Examples

Trimming

Removing leading and trailing whitespace.

let text = "       Hello World!        ";
let result = text.trim();
console.log(result); // "Hello World!"

Padding Start

Padding the start of a string until it reaches length 4.

let text = "5";
let padded = text.padStart(4, "0");
console.log(padded); // "0005"

Padding End

Padding the end of a string.

let text = "5";
let padded = text.padEnd(4, "x");
console.log(padded); // "5xxx"

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.