Substr function of JavaScript also returns the part of a string of specified length starting from the start index. Here specified length means it returns the number of characters. 

Syntax

stringObject.substr([startIndex], optinal [length]);

Substr function takes two parameters one as a starting point from which it starts the sub string. Second parameter is optional that takes as a length of string to be returned by the substr function. If you will not pass the length then it returns the sub string starting from the specified index to the end of provided string.

Example of JavaScript substr function

var myString = new String();

myString = "JavaScript SubStr";

document.write(myString.substr(4, 10));

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
J a v a S c r i p t   S u b S t r
        1 2 3 4 5 6 7 8 9 10      

Example of JavaScript substr with length parameter

var myString = new String();

myString = "JavaScript SubStr";

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

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

Continue to next tutorial: Javascript String Function to learn how to convert an object into string value.

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.