The substring() function extracts a part of a string and returns a new string. It takes two arguments: the start index and the end index (optional). The extracted part includes the characters from the start index to the end index (not including the character at the end index).