Easy Tutorial
❮ Jsref Setutcminutes Prop Element Scrollheight ❯

JavaScript substr() Method

JavaScript String Object

Example

Extract a specified number of characters:

n Output result:


Definition and Usage

The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

Tip: The parameters of substr() specify the starting position and length of the substring, so it can be used as an alternative to substring() and slice().

Note: The substr() method does not change the original string.


Browser Support

All major browsers support the substr() method.


Syntax

Parameter Values

Parameter Description
start Required. The starting index of the substring. It must be a number. If it is negative, the parameter specifies the position from the end of the string. That is, -1 refers to the last character, -2 refers to the second last character, and so on.
length Optional. The number of characters in the substring. It must be a number. If omitted, the method returns the substring from the start position to the end of the string.

Return Value

Type Description
String A new string containing the extracted part of the text.

Technical Details

| JavaScript Version: | 1.0 | | --- | --- |


More Examples

Example

In this example, we will use substr() to extract some characters from the second position of the string:

n Output result:


JavaScript String Object

❮ Jsref Setutcminutes Prop Element Scrollheight ❯