Substring function of JavaScript returns the part of a string starting from the specified starting index to the specified ending index of the provided string.

Syntax

stringObject.substring([startIndex], optional [endIndex]);

Substring function of javascript takes two parameters to return the part of a string. Second parameter is optional. If you will not pass the second parameter then substring function returns the sub string starting from the specified index to the end of string. Whereas if you will pass both the parameters then javascript substring function returns the sub string including the starting index character and excluding ending index character.

Example of JavaScript Substring

var myString = new String();

myString = "JavaScript SubString";

document.write(myString.substring(4, 14));

In the above example starting from 0 as the index of first character of the string you will get the following output result:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
J a v a S c r i p t   S u b S t r i n g

Example of Javascript Substring without end index

var myString = new String();

myString = "JavaScript SubString";

document.write(myString.substring(4));

You will get the returned sub string starting from the passed index to the end of string.

Continue to next tutorial: JavaScript Substr to learn how to get the sub string of specific length starting from specified start index from the provided string.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.