JavaScriptStringsBeginner

String.substring()

Extracts a substring using start/end indexes (no negatives).

Review the syntaxStudy the examplesOpen the coding app
string.substring(start, end?)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Extracts a substring using start/end indexes (no negatives).

The substring() method returns a new string containing the characters of the original string from the specified start index up to, but not including, the end index. A key characteristic of substring() is its argument flexibility: if the start index is greater than the end index, the method automatically swaps them to ensure a valid range (e.g., substring(5, 2) is treated as substring(2, 5)). Additionally, any argument passed as a negative number or NaN is treated as 0. If the end index is omitted, it extracts to the end of the string. In terms of performance, modern JavaScript engines like V8 typically perform a string copy to create the result; while older implementations sometimes used string slicing (referencing the original buffer), modern engines favor copying to prevent memory leaks where a small substring prevents a large parent string from being garbage collected. It is also important to note that if an index exceeds the string length, it is treated as string.length.

Quick reference

Syntax

string.substring(start, end?)

Inputs

Parameters

startnumber · Start index (inclusive).
end (optional)number · End index (exclusive). Defaults to string.length.

See it in practice

Examples

1

Basic Character Extraction

const framework = 'ReactJS';
const result = framework.substring(0, 5);
Output:
"React"

Extracts characters from index 0 to 4. Index 5 is exclusive.

2

Automatic Argument Swapping

const text = 'Frontend';
const result = text.substring(8, 5);
Output:
"end"

Because 8 > 5, substring swaps the arguments and executes as substring(5, 8).

3

Handling Negative and NaN Indices

const platform = 'LearningApp';
const result1 = platform.substring(-5, 8);
const result2 = platform.substring(NaN, 8);
Output:
result1: "Learning", result2: "Learning"

Negative values and NaN are coerced to 0, resulting in an extraction from index 0 to 8.

Debug faster

Common Errors

1

LogicError

Cause: Attempting to use negative indices to count backwards from the end of the string, which works in slice() but not in substring().

Fix: Use the slice() method if you need to use negative offsets, or calculate the index manually using string.length.

'Hello'.substring(-3); // returns 'Hello' (treated as 0)
2

LogicError

Cause: Confusing the second argument of substring() with 'length' instead of an 'end index'.

Fix: Remember that the second argument is the exclusive end position. If you want a specific length, calculate end as (start + length).

'JavaScript'.substring(4, 2); // returns 'va', expected 'Sc' if thinking length

Runtime support

Compatibility

Node.js, BrowserAll browsersES3+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Extracts a substring using start/end indexes (no negatives).

start: Start index (inclusive). end: End index (exclusive). Defaults to string.length.

LogicError: Use the slice() method if you need to use negative offsets, or calculate the index manually using string.length. LogicError: Remember that the second argument is the exclusive end position. If you want a specific length, calculate end as (start + length).