How do I get a substring from a string in JavaScript?

To get a substring from a string in JavaScript, you can use the built-in string.substring(start, end) method. The first parameter specifies the index of the first character to include in the returned substring (indexing starts from zero). The second parameter is optional and specifies the index of the first character to exclude from the returned substring (the character at the specified index is not included in the substring). If the second parameter is not specified, then the substring() method copies all characters up to the end of the string. If the "start" parameter is equal to the "end" parameter, then the substring() method will return an empty string. If the "start" parameter is greater than the "end" parameter, then the method will swap its values. If a negative number, undefined, NaN is specified as indexes values, then this value is considered equal to zero. If one of the parameters is greater than or equal to the length of the string, then the value is considered equal to the length of the string. The substring() function does not change the original string but returns a new string containing the specified part of the given string. Click Execute to run the JavaScript Substring Example online and see the result.
How do I get a substring from a string in JavaScript? Execute
let str = 'JavaScript Substring';

console.log(str.substr(4, 6));
Updated: Viewed: 1230 times

What is substring?

A substring is a part of a string, which can be a single character, the entire string, or any other piece of the string. Substrings are not limited to words and can contain characters from the middle of a word. A substring is also a string, and it can be written to a variable for further work with it. JavaScript has this built-in string.substring() function which returns the substring of a given string.

JavaScript substring() Syntax
string.substring(start, end)

Where:
  • start: an integer specifies the starting position from which to copy the substring from the source string. In JavaScript the indexing starts from zero.
  • end (optional): an integer that specifies the end position up to which the substring is copied. The character at the specified index is not included. If the "end" parameter is not specified, all characters up to the end of the string are copied.

JavaScript Substring Examples

Below are some examples of extracting substrings from strings in JavaScript:

How to extract a substring from the beginning of a string?

The example uses the substring() function to extract a substring starting from the first position of the string:

Extract the First Characters from the String
let str = 'JavaScript Substring';

console.log(str.substring(0, 10));

// output: JavaScript

How to extract the last characters of a string?

The example uses the substring() function to extract a substring from the specified index up to the end of the string:

Example of extracting a substring to the end of a string
let str = 'JavaScript Substring';

console.log(str.substring(11));

// output: Substring

How to extract a substring from the middle of a string?

The example uses the substring() function to extract a substring from the middle of a string:

Extract the middle from the String
let str = 'JavaScript Substring';

console.log(str.substring(4, 10));

// output: Script

How to search for a substring in a string in JavaScript?

To find a substring in a string in JavaScript, you can use the indexOf() method. The indexOf() method returns the first occurrence of the specified substring in the string, or "-1". The method is case-sensitive.

Example of finding a substring in a string
let str = 'JavaScript';

console.log(str.indexOf('Script'));

// output: 4

What is the difference between substr() and substring() in JavaScript?

The string.substr(start, length) method extracts the parts of a string starting with the character at the specified position and returns the specified number of characters. The string.substring(start, end) method returns the part of the string between the start and end indexes or up to the end of the string. The difference is in the second parameter. The second parameter of the substring() function is an index that indicates the end position of the substring. And the second parameter of the substr() function is the number of characters to extract. Also, substr() can accept a negative start position as an offset from the end of the string, but substring() cannot. The substr() function is deprecated in ECMAScript and may be removed in future versions, so it's best to avoid it if possible.

JavaScript substr() Example
let str = 'JavaScript Substring';

console.log(str.substr(4, 6));

// output: Script

What is the difference between slice() and substring() in JavaScript?

The string.slice(start, end) and string.substring(start, end) functions are almost identical, but there is a main difference between them is how they handle negative arguments. The developers couldn't change the behavior of the substring() function to make it work in a new way because that would break backward compatibility with the old scripts. Therefore, a new slice() function has been added to JavaScript.

Function Common Differences
substring(start, end) 1. If start is equal to end: empty string is returned.
2. If end is omitted: extract all characters from the start position to the end of the string.
3. If any of the arguments is greater than the length of the string, the length of the string will be used instead.
4. Do not change the original string, but return a new string.
1. If start > end, then substring() will swap these 2 arguments.
2. If any of the arguments is negative or equal to NaN, it is treated as if it were negative 0.
slice(start, end) 1. If start > end, then slice() will return an empty string.
JavaScript slice() Example
let str = 'JavaScript Substring';

console.log(str.slice(-3, -1));

// output: in

See also