The JavaScript substr() String Method

By: Dusty Arlia
Published on Wednesday, April 3, 2013, 01:32 PM
Last Updated on Thursday, July 16, 2015 at 8:25 PM
Total Updates: 2

The JavaScript substr() string method extracts a range of characters from a string. The substr() method is passed two values: the first number is an index number where to start the extraction and the second number is the amount of characters to extract after the index number. Keep in mind that the first character in the string is at index 0. If the second number is missing, then the substr() method returns the rest of the string past the index that was given. The substr() method does not actually change the original string. It just returns a portion of it.

Here is an example:

var example_string = "My name is Dusty";
var first_name = example_string.substr(11,3);

In this example Dus is written to the screen.