.substring()

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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy