JavaScript String Methods

JavaScript strings come with many built-in methods for searching, extracting, and transforming text.

Working with String Methods

A string method is a built-in function attached to every string value. You call it with dot notation after the string. Because strings are immutable, these methods never modify the original text — they return a new value that you can store in a variable or use directly.

  • Changing case: toUpperCase() and toLowerCase()
  • Trimming whitespace: trim(), trimStart(), trimEnd()
  • Searching: indexOf(), includes(), startsWith(), endsWith()
  • Extracting: slice(), substring(), charAt()
  • Replacing and splitting: replace(), replaceAll(), split()

Case and whitespace

<!DOCTYPE html>
<html>
<body>

<h2>Case and whitespace</h2>

<script>
let raw = "   Hello World   ";

console.log(raw.trim());          // "Hello World"
console.log(raw.trim().toUpperCase()); // "HELLO WORLD"
console.log("HELLO".toLowerCase());     // "hello"

// The original is unchanged
console.log(raw); // "   Hello World   "
</script>

</body>
</html>

To find text inside a string, indexOf() returns the position of the first match, or -1 when nothing is found. When you only care whether the text exists, includes() returns a simple true or false, which reads more clearly in conditions.

Searching within a string

<!DOCTYPE html>
<html>
<body>

<h2>Searching within a string</h2>

<script>
let sentence = "The quick brown fox";

console.log(sentence.indexOf("quick"));   // 4
console.log(sentence.indexOf("cat"));     // -1
console.log(sentence.includes("fox"));    // true
console.log(sentence.startsWith("The"));  // true
console.log(sentence.endsWith("dog"));    // false
</script>

</body>
</html>
Note: slice() accepts negative indexes, which count back from the end of the string. "JavaScript".slice(-6) returns "Script". substring() does not support negatives and treats them as 0.

Extracting and replacing

<!DOCTYPE html>
<html>
<body>

<h2>Extracting and replacing</h2>

<script>
let text = "JavaScript";

console.log(text.slice(0, 4));  // "Java"
console.log(text.slice(-6));    // "Script"

let msg = "I like cats, cats are great";
console.log(msg.replace("cats", "dogs"));    // replaces first only
console.log(msg.replaceAll("cats", "dogs")); // replaces every match

console.log("a,b,c".split(",")); // ["a", "b", "c"]
</script>

</body>
</html>

Because each method returns a string, you can chain several calls together. Each step operates on the result of the previous one, which lets you clean and reshape text in a single expression.

Note: replace() only swaps the first occurrence when given a plain string. Use replaceAll(), or a regular expression with the global flag, to change every match.
MethodWhat it doesExample
toUpperCase()Converts all characters to uppercase"hi".toUpperCase() → "HI"
toLowerCase()Converts all characters to lowercase"HI".toLowerCase() → "hi"
trim()Removes whitespace from both ends" hi ".trim() → "hi"
indexOf()Position of first match, or -1"abc".indexOf("b") → 1
includes()Whether the text contains a substring"abc".includes("a") → true
slice(start, end)Extracts a section into a new string"abcd".slice(1, 3) → "bc"
replace()Returns a new string with the first match swapped"aa".replace("a", "b") → "ba"
split()Splits a string into an array"a-b".split("-") → ["a", "b"]