Featured Article

Master JavaScript Substring with W3Schools: A Complete Guide

Adam Jul 13, 2026

The substring method in JavaScript is a powerful tool for extracting sections of strings, and it's widely used in web development. Understanding how to use it can greatly enhance your ability to manipulate and transform textual data in your scripts.

Javascript String Methods
Javascript String Methods

W3Schools, a renowned learning resource for web technologies, provides an excellent reference for the substring method. Let's explore this method's syntax, parameters, return values, and usage with practical examples, making use of W3Schools' resources.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Understanding the substring() Method

quarta

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

The substring() method extracts a section of a string between two given indices. It's important to note that these indices are positional, non-negative, and are zero-based; that is, the first character's index is 0.

Syntax and Parameters

JavaScript String Methods 🧲
JavaScript String Methods 🧲

First, let's understand the syntax and parameters of the substring() method. According to W3Schools:

stringObject.substring(indexStart, indexEnd)

  • indexStart: An integer representing the index from which to start extracting. If not specified, it defaults to 0.
  • indexEnd: An integer representing the index at which to stop extracting. If not specified, it extracts the rest of the string from the start index.

Important Points to Consider

Essential JavaScript String Methods for Beginners
Essential JavaScript String Methods for Beginners

There are two essential points to consider when using substring():

  • Indices are inclusive, meaning the character at the start index is included, but the character at the end index is excluded. To extract a section of length n, you should provide an end index of n more than the start index.
  • If you pass a negative index, it will be treated as an index from the end of the string.

Using substring() Method: W3Schools Examples

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

Now that we understand the basics of substring() let's turn to W3Schools for some practical examples.

Extracting Part of a String

String methods in JavaScript!
String methods in JavaScript!
Javascript Cheet cheet
Javascript Cheet cheet
Java script string method
Java script string method
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
TrimEnd JavaScript Trimming Strings Made Easy
TrimEnd JavaScript Trimming Strings Made Easy
an info sheet with different types of web pages
an info sheet with different types of web pages
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript switch Statement: Simplify Multiple Conditions 🔄
JavaScript switch Statement: Simplify Multiple Conditions 🔄
JavaScript Basics: The Essential Guide for Beginners 🚀
JavaScript Basics: The Essential Guide for Beginners 🚀
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
All important string methods in JavaScript 2024
All important string methods in JavaScript 2024
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Using switch Statements in JavaScript
Using switch Statements in JavaScript
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
Synchronous & Asynchronous Javascript
Synchronous & Asynchronous Javascript
an open notebook with some type of function written in red on the top and bottom
an open notebook with some type of function written in red on the top and bottom
javascript cheetsheet
javascript cheetsheet
Permutations & Combinations in Javascript
Permutations & Combinations in Javascript
an image of a web page with different types of text
an image of a web page with different types of text

W3Schools demonstrates how to extract part of a string using substring(). Here's one of their examples:

var str = "Hello world!"; var res = str.substring(0, 5);

Extracting with Negative Indices

W3Schools also shows how to use the method with negative indices:

var str = "Hello world!"; var res = str.substring(-5);

Extracting Beyond the End

Although it might seem counterintuitive, you can use substring() to extract a section that goes beyond the string's end:

var str = "Hello world!"; var res = str.substring(7, 17);

Practical Applications of substring()

Now that we've explored the substring() method let's look at some practical applications:

Limiting Text Length

You can use substring() to limit the length of a displaying text, such as a review or comment, to a certain number of characters:

var text = "This is a very long review..."; var trimmedText = text.substring(0, 100);

Extraction based on textual patterns

The substring() method can also be used in combination with other methods, such as indexOf or lastIndexOf, to extract parts of a string based on textual patterns:

var str = "Visit Microsoft.com today!"; var pos = str.indexOf("Microsoft"); var res = str.substring(pos, pos + 10);

Finally, remember that JavaScript also offers an alternative method for string extraction - slice(). While substring() is limited to extracting parts based on single indices, slice() can use step values to extract parts of an array-like sequence of characters.

Keep practicing with W3Schools' examples and expand your knowledge of JavaScript string methods. Happy coding!