freecoding.school100% FREE · NO SIGNUP
Script SpringsISSUE #13 of 29

string methods · slice · split · replace

SparkyVSNaN the Trickster
Sparky saysString methods: slice, split, replace/replaceAll, includes, trim, toUpperCase, padStart.

The toolkit: slice(start,end) extracts; split(sep) makes an array; replace/replaceAll substitute; includes/startsWith/endsWith test; trim removes edge whitespace; padStart/padEnd pad. All return new strings (immutable).

Power-ups you unlock

NaN the Trickster attacks — common mistakes

Boss battleTake a messy string, trim it, uppercase it, and replace all spaces with dashes.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const s='  hello world  ';
document.getElementById('o').textContent = s.trim().toUpperCase().replaceAll(' ','-');</script></body></html>
▶ Open the interactive comic issue
‹ Template Literals · InterpolationBooleans · Truthy · Falsy ›