JavaScript String substr()
Syntax & Examples
String.substr() method
The substr() method of the String class in JavaScript returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.
Syntax of String.substr()
There are 2 variations for the syntax of String.substr() method. They are:
substr(start)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
start | required | The index of the first character to include in the returned substring. A negative index counts back from the end of the string. |
This method returns a portion of the string starting from the specified index to the end of the string.
Returns value of type String.
substr(start, length)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
start | required | The index of the first character to include in the returned substring. A negative index counts back from the end of the string. |
length | optional | The number of characters to include in the returned substring. If omitted, it extracts characters to the end of the string. |
This method returns a portion of the string starting from the specified index and extending for the specified number of characters.
Returns value of type String.
✐ Examples
1 Using substr() method with only the start index
In JavaScript, we can use the substr() method to extract a substring starting from the specified index to the end of the string.
For example,
- We define a string variable
strwith the value'Hello, world!'. - We use the
substr()method with the start index7. - The extracted substring,
'world!', is stored in the variablesubStr. - We log
subStrto the console usingconsole.log()method.
JavaScript Program
const str = 'Hello, world!';
const subStr = str.substr(7);
console.log(subStr);Output
'world!'
2 Using substr() method with start index and length
In this example, we use the substr() method to extract a substring starting from the specified index and extending for the specified number of characters.
For example,
- We define a string variable
strwith the value'Hello, world!'. - We use the
substr()method with the start index7and the length5. - The extracted substring,
'world', is stored in the variablesubStr. - We log
subStrto the console usingconsole.log()method.
JavaScript Program
const str = 'Hello, world!';
const subStr = str.substr(7, 5);
console.log(subStr);Output
'world'
3 Using substr() method with a negative start index
This example demonstrates how to use the substr() method with a negative start index to extract a substring from the end of the string.
For example,
- We define a string variable
strwith the value'Hello, world!'. - We use the
substr()method with the start index-6to extract the last six characters. - The extracted substring,
'world!', is stored in the variablesubStr. - We log
subStrto the console usingconsole.log()method.
JavaScript Program
const str = 'Hello, world!';
const subStr = str.substr(-6);
console.log(subStr);Output
'world!'
Summary
In this JavaScript tutorial, we learned about substr() method of String: the syntax and few working examples with output and detailed explanation for each example.