JavaScript string methods are built-in functions you call on a string value to inspect or transform it without changing the original string. The shortest working set: create a string with const s = "Hello World";, then call methods like s.toUpperCase(), s.includes("World") or s.slice(0, 5). Strings are immutable, so every method returns a new value — you must assign or log the result to see it.

What You'll Need

  • A modern browser (Chrome, Edge, Firefox or Safari) with the developer console open, or Node.js installed if you prefer a terminal.
  • A basic understanding of variables and console.log().
  • Optional: a code editor such as VS Code for longer experiments.

Step-by-Step

  1. Open a place to run code. In a browser, press F12 (or right-click the page and choose Inspect) and switch to the Console tab. In Node.js, run node in a terminal. Menu labels for developer tools can differ slightly by browser version and platform.
  2. Create a string and check its length. Type const s = "Smart Home"; then s.length. Length counts every character including spaces, so the result is 10.
  3. Change case. Run s.toUpperCase() and s.toLowerCase(). These return new strings; s itself stays the same.
  4. Find text inside a string. Use s.includes("Home") for a true/false answer, s.indexOf("Home") for the starting position (6), and s.startsWith("Smart") or s.endsWith("Home") for edge checks. All are case-sensitive.
  5. Extract part of a string. Use s.slice(0, 5) to get characters from index 0 up to but not including 5 ("Smart"). Negative indexes count from the end: s.slice(-4) returns "Home". substring(start, end) works similarly but treats negative numbers as 0.
  6. Replace text. s.replace("Home", "Hub") swaps the first match only. To replace every match, use a global regular expression: "a-b-a".replace(/a/g, "x"). replaceAll("a", "x") also replaces every plain-text match in modern browsers.
  7. Trim whitespace. " hi ".trim() removes spaces from both ends. trimStart() and trimEnd() target one side only. This is useful before comparing user input.
  8. Split and join. "a,b,c".split(",") returns an array ["a", "b", "c"]. Arrays have join(), so ["a","b"].join("-") returns "a-b". Strings do not have join; convert with split first.
  9. Repeat and pad. "ab".repeat(3) gives "ababab". "7".padStart(3, "0") gives "007", handy for formatting numbers.
  10. Access characters. Use bracket notation s[0] or s.charAt(0) for a single character. s.charCodeAt(0) returns its numeric code.
  11. Chain methods. Because each method returns a string, you can chain: s.trim().toLowerCase().replace(" ", "-"). Read left to right; each step receives the previous result.
  12. Log and verify. Wrap results in console.log() and compare against the original variable to confirm the source string was not modified.

Common Mistakes

  • Expecting the original to change. s.toUpperCase() alone does nothing visible; assign it: const upper = s.toUpperCase();.
  • Forgetting case sensitivity. "Home".includes("home") is false. Lowercase both sides first when matching user input.
  • Off-by-one slicing. slice(0, 5) returns five characters, not six. The end index is exclusive.
  • Assuming replace replaces all. A plain string pattern replaces only the first occurrence.
  • Calling array methods on strings. join, push and map belong to arrays. Convert with split first.
  • Using substring with negative indexes. It clamps them to 0; use slice for negative offsets.

Tips

  • Use template literals (`Hello ${name}`) for building strings instead of repeated concatenation.
  • Prefer includes() over indexOf() !== -1 for readability.
  • Use localeCompare() for sorting strings alphabetically in a user's language rather than < and >.
  • Keep a scratch console tab open while learning; experimenting is faster than memorizing.
  • Check the official MDN Web Docs reference for exact behavior of any method before relying on it in production.

Troubleshooting

  • "undefined is not a function". The value is probably not a string. Check with typeof value; convert with String(value).
  • Result looks unchanged. You likely logged the original variable instead of the returned value. Log the method call directly.
  • Unexpected characters. Hidden whitespace or non-breaking spaces may be present. Run trim() and inspect with JSON.stringify(s) to reveal invisible characters.
  • Method not recognized in an old browser. Newer methods such as replaceAll may not exist in very old environments. Use a regular expression with the g flag as a fallback, or update the browser.
  • Console shows nothing. Refresh the page and reopen the console; some browsers clear it on navigation. If it still fails, restart the browser or Node process.

Frequently Asked Questions

Do string methods change the original string?

No. JavaScript strings are immutable, so every method returns a new string. You must assign the result to a variable or use it directly.

What is the difference between slice and substring?

Both extract a section, but slice accepts negative indexes (counting from the end) while substring treats negatives as 0 and swaps start and end if start is greater.

How do I replace every occurrence of a word?

Use replaceAll("word", "new") in modern browsers, or replace(/word/g, "new") with a global regular expression.

How do I check if a string contains another string, ignoring case?

Lowercase both values first: s.toLowerCase().includes(term.toLowerCase()).

Can I use string methods on numbers?

Not directly. Convert first with String(42) or (42).toString(), then call methods like padStart.

Where can I find the full list of string methods?

The MDN Web Docs page for String lists every method with examples and browser support.