Skip to main content

Substrings

A substring is a portion of a original string. For example, "script" is a substring of "javascript" because by taking last six characters we get "script".

Substring signature

Let's take a look at the syntax and parameters of a substring method:

string.substring(indexStart, indexEnd);

The substring method have two parameters,

  • indexStart: The position of the start of the substring. This value is included.
  • indexEnd: The position of the end of the substring. This value is ignored.

Example

const name = "CodingHabits";
console.log(name.substring(6, 11));
Note

The indexEnd parameter is optional. If you pass only one argument, it will be consider as indexStart in default. It will assume indexEnd to be the length of the current string.