.substring()

EugeneGoh_'s avatar
Published Jun 21, 2021Updated Aug 8, 2023
Contribute to Docs

The .substring() method returns a part of a string from a given starting index or between the start and end index. The index starts at zero.

Syntax

myString.substring(startIndex, endIndex);
  • startIndex is the starting index from which the returned substring will cover.
  • endIndex is optional. It defaults to cover the rest of the string.

Example

Returning a part of a string from a given starting index:

console.log('Do you love JavaScript or Python?'.substring(2));
// Output: you love JavaScript or Python?

Codebyte Example

Returning a part of a string between start and end index:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy