JavaScript String Methods

slice, replace, split, trim, includes and more.

✍️ 3 exercises + code challenge in this lesson

Strings come with many built-in methods. Strings are immutable: methods return a new string and never change the original.

  • toUpperCase(), toLowerCase()
  • slice(start, end), substring()
  • replace(), replaceAll()
  • trim(): remove spaces at both ends
  • split(","): turn a string into an array
  • includes(), startsWith(), endsWith(), indexOf()
  • padStart(), repeat(), at(-1)

Key Takeaways: JavaScript String Methods

Mastering JavaScript String Methods helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript String Methods Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
const text = "  Learn JavaScript at CodesInTech  ";
const clean = text.trim();

log(clean.toUpperCase());
log(clean.slice(6, 16));
log(clean.replace("JavaScript", "Python"));
log(clean.includes("Code"), clean.startsWith("Learn"));
log("Last char:", clean.at(-1));
log("a,b,c".split(","));
log("7".padStart(3, "0"));
log("ha".repeat(3));
</script>
Free sign-in required · Google login available

JavaScript String Methods Practice Exercises

Check what you learned in “JavaScript String Methods”.

/ 3 solved
Fill in the blank

Convert the text to upper case.

const loud = text.();
✓ Correct! Not quite. Try again.
Multiple choice

Which method removes spaces from both ends of a string?

✓ Correct! Not quite. Try again.
Code challenge

Replace "cats" with "dogs" in the sentence and log the result.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services