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
- 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
nodein a terminal. Menu labels for developer tools can differ slightly by browser version and platform. - Create a string and check its length. Type
const s = "Smart Home";thens.length. Length counts every character including spaces, so the result is 10. - Change case. Run
s.toUpperCase()ands.toLowerCase(). These return new strings;sitself stays the same. - Find text inside a string. Use
s.includes("Home")for a true/false answer,s.indexOf("Home")for the starting position (6), ands.startsWith("Smart")ors.endsWith("Home")for edge checks. All are case-sensitive. - 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. - 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. - Trim whitespace.
" hi ".trim()removes spaces from both ends.trimStart()andtrimEnd()target one side only. This is useful before comparing user input. - Split and join.
"a,b,c".split(",")returns an array["a", "b", "c"]. Arrays havejoin(), so["a","b"].join("-")returns "a-b". Strings do not havejoin; convert withsplitfirst. - Repeat and pad.
"ab".repeat(3)gives "ababab"."7".padStart(3, "0")gives "007", handy for formatting numbers. - Access characters. Use bracket notation
s[0]ors.charAt(0)for a single character.s.charCodeAt(0)returns its numeric code. - 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. - 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
replacereplaces all. A plain string pattern replaces only the first occurrence. - Calling array methods on strings.
join,pushandmapbelong to arrays. Convert withsplitfirst. - Using
substringwith negative indexes. It clamps them to 0; useslicefor negative offsets.
Tips
- Use template literals (
`Hello ${name}`) for building strings instead of repeated concatenation. - Prefer
includes()overindexOf() !== -1for 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 withString(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 withJSON.stringify(s)to reveal invisible characters. - Method not recognized in an old browser. Newer methods such as
replaceAllmay not exist in very old environments. Use a regular expression with thegflag 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.