String Functions in JavaScript: The Essential Guide

Formulas & functions · Updated November 22, 2026

String functions in JavaScript are built-in methods that let you manipulate and inspect text values, from finding and replacing substrings to changing case and trimming whitespace. This guide covers the most useful string methods, how they work, and common pitfalls to avoid.

What Are String Methods?

In JavaScript, strings are primitive values, but they come with a set of built-in methods (often called functions) that operate on the string. These methods are accessed with dot notation, like myString.length or myString.toUpperCase(). They are immutable, meaning they do not change the original string; instead, they return a new string. For example:

let greeting = "Hello";
let loud = greeting.toUpperCase();
console.log(loud); // "HELLO"
console.log(greeting); // "Hello" — original unchanged

Essential String Methods You’ll Use Daily

Here are the most commonly used string methods, grouped by what they do:

  • Case conversion: toUpperCase() and toLowerCase() return the string in all caps or all lowercase.
  • Searching: indexOf(), lastIndexOf(), includes(), startsWith(), and endsWith() help you find substrings or check if a string contains something.
  • Extracting parts: slice(), substring(), and substr() (deprecated) pull out a portion of the string.
  • Replacing: replace() and replaceAll() swap a substring with another.
  • Splitting and joining: split() turns a string into an array; join() (on an array) does the reverse.
  • Trimming: trim(), trimStart(), and trimEnd() remove whitespace from the ends.
  • Padding: padStart() and padEnd() add characters to reach a target length.
  • Repeating: repeat() returns the string repeated a specified number of times.

How to Use Them: Examples with Code

Let’s see some in action. Assume you have a variable let str = "The quick brown fox";

  • Find the position of a word: str.indexOf("quick") returns 4 (0-based index). If not found, returns -1.
  • Check if a string contains a substring: str.includes("fox") returns true.
  • Extract a portion: str.slice(4, 9) returns "quick". Note that slice() accepts negative indices (count from end).
  • Replace the first occurrence: str.replace("quick", "slow") returns "The slow brown fox". To replace all, use replaceAll() or a global regex.
  • Split into words: str.split(" ") returns ["The", "quick", "brown", "fox"].

String vs. Array Methods: When to Use Which

Strings and arrays share some method names (like slice(), includes(), indexOf()), but they are distinct. The key difference: string methods work on characters, array methods on elements. For instance, split() is a string method that returns an array; join() is an array method that returns a string. If you need to manipulate characters individually, converting to an array with split('') might be easier, but be careful with Unicode characters (see below).

Common Pitfalls and Misconceptions

Several mistakes trip up beginners and even experienced developers:

  • Mutating strings: Remember, methods don’t change the original string. If you want to keep the result, assign it to a variable.
  • Case sensitivity: includes() and indexOf() are case-sensitive. Use toLowerCase() on both sides for case-insensitive checks.
  • Unicode and emoji: JavaScript strings are UTF-16. Methods like length and slice() count code units, not characters. An emoji like 😀 is two code units, so "😀".length returns 2. For accurate character counting, use Array.from() or the Intl.Segmenter API.
  • Using substr(): It’s deprecated; use slice() or substring() instead.
  • Regular expressions with replace(): If you pass a string as the first argument, only the first occurrence is replaced. To replace all, use a regex with the g flag or replaceAll().

Performance and Efficiency Considerations

For most everyday tasks, string methods are fast enough. However, if you’re doing heavy string concatenation in a loop, using += can be slower than building an array and joining. Modern JavaScript engines optimize += well, but for thousands of iterations, consider using an array and join(). Additionally, methods like split() and replace() with complex regexes can be costly; if performance matters, test with your data.

Alternatives: Regular Expressions and Template Literals

Sometimes string methods aren’t enough. Regular expressions (regex) allow complex pattern matching and replacement. For example, to replace all digits in a string: str.replace(/\d/g, '#'). Template literals (backticks) provide a cleaner way to build strings with variables: `Hello, ${name}!` instead of concatenation.

Frequently Asked Questions

How do I check if a string contains a substring in JavaScript?

Use the includes() method: "Hello world".includes("world") returns true. It’s case-sensitive, so convert both to lowercase if you want case-insensitive matching.

What is the difference between slice() and substring()?

Both extract a part of a string, but slice() accepts negative indices (counting from the end) and treats them as offsets from the end. substring() treats negative values as 0 and swaps arguments if the first is greater than the second. In practice, slice() is more predictable and recommended.

Why does my string method not change the original string?

JavaScript strings are immutable. Methods return a new string; the original remains unchanged. You must assign the result to a variable: let newStr = oldStr.toUpperCase();

How do I replace all occurrences of a substring?

Use replaceAll() (ES2021) or a global regex: str.replace(/old/g, 'new'). If you pass a string to replace(), only the first occurrence is replaced.

How can I count the number of characters in a string with emojis?

Use Array.from(str).length or the Intl.Segmenter API to count grapheme clusters correctly. The built-in length property counts UTF-16 code units, which can be double for emojis.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.