substring() in javascript select a part of the string and return a substring. In this article, we will learn to work with strings using the substring() method.
You will learn what is substring and learn to extract a substring from a string by applying different methods. We will discuss substring and several ways to extract a substring from a string
Table of Contents
What is Substring in JavaScript?
- Consider a string
Hello Everyone. In the stringHello Everyone, The stringHellois a substring ofHello Everyone. Also, the stringEveryoneis a substring of the stringHello Everyone. - Therefore, a small part of a string can be called a substring. The length of a substring can vary from 1 character to the entire length of the original string
Note: Remember that only the part of a continuous sequence of characters can be considered a substring
- For example, The substrings of the string
quedemycould be:
'quedemy',
'que', 'demy',
'qu', 'ue', 'ed', 'em', 'my',
'q', 'u', 'e', 'd', 'e', 'm', 'y'Get Substring in Javascript
We can use the following methods to extract substring in javascript: substring() method and slice() method. Let's discuss in details.
The substring() Method
- We can use built-in Javascript
substring()method to get substrings from a given string. - The syntax for the
substring()method is:substring(indexStart, indexEnd) substring()method can accept two arguments asindexStartandindexEnd. Then, thesubstring()method returns a substring from the starting index to the ending index.- Returned result from
substring()method includes character of start index and does not include character of last index.
const exStr = 'E-learning'
// 1. Get substring from startIndex '0' to endIndex '6'
console.log(exStr.substring(0, 7)) // output: E-learn
// 2. Get substring from startIndex '3' to endIndex '6'
console.log(exStr.substring(3, 7)) // output: earn- In the above code, we have a string
E-learning. We applied thesubstring()method to get the substringE-learnandearn. endIndexis optional parameter. If the value ofendIndexis not specified, it extracts all characters till the end of the string
const exStr = 'E-learning'
// get substring from startIndex '3' till the end
console.log(exStr.substring(3)) // output: earningNote:
indexStartwill be treated as 0, if value ofindexStartisundefined,NaN, or not specified. Therefore, the substring() method returns the whole string.const exStr = 'E-learning' console.log(exStr.substring(undefined)) // output: E-learning console.log(exStr.substring(NaN)) // output: E-learning console.log(exStr.substring()) // output: E-learning
- If value of
indexStartis equal to the value ofindexEnd,substring()method will return an empty string.const exStr = 'E-learning' console.log(exStr.substring(2, 2)) // output: ""
The slice() Method
slice()is also a built-in method in javascript and returns a part of the string- The syntax for the
slice()method is:slice(indexStart, indexEnd)
const str = 'Crack Interviews'
// Return a substring from index 0 to index 4
console.log(str.slice(0, 5)) // output: Crack
// Return a substring from index 2 to index 3
console.log(str.slice(2, 4)) // output: ac