Account
Categories

JavaScript substring() Method


Definition:

When you use this function in a program, it takes a string and returns a part based on the given start and end positions.

It starts from the start index and stops before the end index.

Syntax:

stringVariable.substring(startIndex, endIndex)

Explanation:

Whereas,

  • string → A type of data that stores text.
  • variable → Used to store a value.
  • length → Used to find the size of a string (total number of characters).
  • substring(startIndex, endIndex) → Used to get a part of the string based on the given start and end positions.

Example:

let name = "edutation.com";
console.log(name.substring(0, 3)); // "edu"

Output: