How to extract a substring in Javascript

Editorial Team ︱ December 9, 2025

Working with strings is a fundamental part of JavaScript programming. Whether you’re parsing user input, cleaning up data, or manipulating dynamic content, understanding how to extract substrings from larger strings is an essential skill. JavaScript provides several methods to accomplish this, each with its own quirks and ideal use cases.

TL;DR

JavaScript offers multiple ways to extract substrings, including slice(), substring(), and substr(). The slice() method is generally preferred due to its simplicity and flexibility with negative indices. While substring() behaves similarly, it doesn’t accept negative values. Note that substr() is deprecated and should be avoided in new code. Choosing the right method depends on whether you’re specifying start and end positions or a start position with a length.

Understanding Substrings in JavaScript

In JavaScript, a substring refers to a portion of a string extracted based on specific start and end positions. Unlike in some other programming languages, JavaScript strings are immutable, meaning methods that extract substrings return new strings rather than modifying the original.

There are three primary methods to extract substrings in JavaScript:

  • slice(start, end)
  • substring(start, end)
  • substr(start, length) (deprecated)

1. Using slice()

The slice() method is commonly used because it is simple and supports negative indices. This makes it versatile for both extracting substrings from the beginning and end of a string.

Syntax:

string.slice(start, end)

Parameters:

  • start: Index at which to start extraction
  • end: (Optional) Index before which to end extraction

If end is omitted, slice() extracts to the end of the string. It also accepts negative indices, which count from the end of the string.

Example:

let text = "Hello, world!";
let result = text.slice(7, 12);  // Returns "world"

Using negative indices:

let result = text.slice(-6, -1);  // Returns "world"

This makes slice() very powerful for flexible string manipulation.

2. Using substring()

The substring() method is similar to slice(), but with notable differences:

  • It does not accept negative indices.
  • If start is greater than end, the method swaps them automatically.

Syntax:

string.substring(start, end)

Example:

let text = "JavaScript";
let result = text.substring(4, 10); // Returns "Script"

If you try negative values:

let result = text.substring(-2, 4); // Negative values are treated as 0; returns "Java"

This can lead to unexpected results if you’re not careful. For this reason, slice() is often preferred unless you specifically need substring()‘s behavior.

3. Using substr() – Not Recommended

Warning: The substr() method is considered deprecated and should not be used in new projects. While still available in most browsers, it may be removed in future versions of JavaScript.

Unlike slice() and substring(), substr() uses a length value instead of an end position.

Syntax:

string.substr(start, length)

Example:

let text = "Extract this text";
let result = text.substr(8, 4); // Returns "this"

Although this method may seem intuitive, especially for beginners, its deprecation means it’s best to stick with slice() for readability and future compatibility.

Practical Examples

Extracting Domain from URL

let url = "https://www.example.com";
let domain = url.slice(8); // "www.example.com"
let domainOnly = domain.slice(0, domain.indexOf(".")); // "www"

In this example, we use slice() to remove the protocol part of the URL and then extract the first part of the domain using indexOf().

Extracting File Extension

let filename = "photo.jpeg";
let extension = filename.slice(filename.lastIndexOf('.') + 1); // "jpeg"

By combining lastIndexOf() with slice(), you can reliably extract file extensions regardless of filename length.

Getting Initials from Name

let name = "John Doe";
let initials = name[0] + name[name.indexOf(" ") + 1]; // "JD"

This example uses character indexing and indexOf() to grab the initials from a full name.

Performance Considerations

For small- to medium-sized strings, performance probably won’t be an issue, and you can freely choose the most intuitive method. However, for large-scale string processing—such as text parsing, data cleaning, or DOM manipulation—it’s important to be clear and consistent in using the optimal method.

  • Use slice() for most scenarios due to its flexibility and support for negative numbers.
  • Avoid substr() due to its deprecated status.
  • Use substring() if you are dealing exclusively with non-negative indices and want automatic index swapping.

Common Pitfalls

Beware of these common mistakes:

  1. Trying to use negative indices with substring(), which treats them as 0.
  2. Using substr() and forgetting its second parameter is length-based, not based on end index.
  3. Swapping start and end parameters manually when substring() could do it automatically (if used intentionally).

Which Method Should You Use?

Your ideal choice depends on your specific use case:

  • slice() is the most versatile and commonly used for both positive and negative index manipulation.
  • substring() is handy if you prefer the automatic swapping of parameters — though its lack of support for negative values limits its flexibility.
  • substr() might look tempting if you’re working with start-and-length patterns, but its deprecation means you should avoid it for any forward-looking code.

As a general rule, unless you have a good reason to use another method, choose slice() — it’s flexible, clear, and widely supported.

In Summary

Extracting substrings in JavaScript isn’t hard, but choosing the right tool can save you time and avoid bugs.

  • Use slice(start, end) when you want versatility and compatibility with modern JavaScript coding standards.
  • Remember: slice supports negative indices, substring does not, and substr is deprecated.
  • Think carefully about your parameters — are you defining a range or a start-and-length?

Now that you know how to extract substrings in JavaScript, you can manipulate strings more precisely in any project — from basic scripts to advanced applications!

Leave a Comment