Getting Substring from 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 from which to get the substring (in JavaScript, indexing starts from zero). The second optional parameter specifies the index of the first character to exclude from the returned substring. If the second parameter is not specified, then the substring() method copies all characters up to the end of the string. The substring() function does not alter the original string but returns the substring from the given string. You can also get a substring from a string using the string.substr(start, length) method, which takes the number of characters to extract as its second argument. There is also a string.slice(start, end) method, which is almost identical to string.substring(), but differs in the way it handles negative argument values. In this JavaScript Substring example, we use the string.substring(start, end) method to return a substring from a string. Click Execute to run the JavaScript Substring Example online and see the result.
Getting Substring from String in JavaScript Execute
let str = 'JavaScript Substring';

console.log(str.substring(4, 10));
Updated: Viewed: 10168 times

What is String in JavaScript?

JavaScript strings are sequences of characters, including letters, numbers, and symbols. Unicode characters can be encoded in JavaScript strings using the "\uXXXX" syntax. In JavaScript, strings are primitive and immutable data types, which means they cannot be changed after they have been created. JavaScript provides many sets of built-in functions for concatenating, containing, splitting, reversing, and trimming strings. You can also convert string to array, check if it starts or ends with another string, and get the length of a string.

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 the same methods can be used for a substring as for a string. 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: a number specifies the starting position from which to copy the substring from the source string. In JavaScript, the indexing starts from zero.
  • end (optional): a number indicating the end position up to which the substring is copied

JavaScript Substring Examples

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

Extracting a substring from the beginning of a string

The following is an example of extracting 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

Extracting the last characters of a string

The following is an example of extracting 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

Extracting a substring from the middle of a string

The following is an example of extracting a substring from the middle of a string:

Extract the substring from the middle of 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 difference between string.substr(start, length) and string.substring(start, end) is in the second parameter. The second parameter for the string.substr() method is the number of characters to extract, and for the string.substring() is the index up to which to extract. Also, substr() can take a negative starting position as an offset from the end of the string, but substring() cannot. The substr() function is deprecated and may be removed in future versions of JavaScript.

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) methods are almost identical, but they differ in handling negative arguments.

Method Common Differences
string.substring(start, end) 1. If the start is equal to the end an empty string is returned.
2. If the end is omitted, it will 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 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.
string.slice(start, end) 1. If start > end, then slice() will return an empty string.
Extracting a substring from a string using the slice() method

The following is an example of how to extract a substring from a string using the slice() method:

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

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

// output: in

See also