<picture><source type="image/webp" srcSet="/static/927f3c1f1737834cdbd785e1efd697c0/94e0c/quedemy-text-logo-rect-violet.webp 45w,/static/927f3c1f1737834cdbd785e1efd697c0/c07b1/quedemy-text-logo-rect-violet.webp 90w,/static/927f3c1f1737834cdbd785e1efd697c0/a810a/quedemy-text-logo-rect-violet.webp 180w,/static/927f3c1f1737834cdbd785e1efd697c0/1ff69/quedemy-text-logo-rect-violet.webp 360w" sizes="(min-width: 180px) 180px, 100vw"/><img data-gatsby-image-ssr="" data-main-image="" style="opacity:0" sizes="(min-width: 180px) 180px, 100vw" decoding="async" loading="lazy" src="/static/927f3c1f1737834cdbd785e1efd697c0/a4dc7/quedemy-text-logo-rect-violet.png" srcSet="/static/927f3c1f1737834cdbd785e1efd697c0/201a0/quedemy-text-logo-rect-violet.png 45w,/static/927f3c1f1737834cdbd785e1efd697c0/93883/quedemy-text-logo-rect-violet.png 90w,/static/927f3c1f1737834cdbd785e1efd697c0/a4dc7/quedemy-text-logo-rect-violet.png 180w,/static/927f3c1f1737834cdbd785e1efd697c0/4d66d/quedemy-text-logo-rect-violet.png 360w" alt="quedemy-logo"/></picture>
Quedemy - HomeQuedemy - Home
Blogs

Javascript substring() Method

Learn Node.js unit testing with Jest, using mocking, snapshots, and best practices to ensure reliable and efficient application performance.

May 20, 202520 min read
Javascript substring() Method

Table of Contents

  • What is Substring in JavaScript?
  • Get Substring in Javascript

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

What is Substring in JavaScript?

  • Consider a string Hello Everyone. In the string Hello Everyone, The string Hello is a substring of Hello Everyone. Also, the string Everyone is a substring of the string Hello 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 quedemy could 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 as indexStart and indexEnd. Then, the substring() 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 the substring() method to get the substring E-learn and earn.
  • endIndex is optional parameter. If the value of endIndex is 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: earning

Note:

  • indexStart will be treated as 0, if value of indexStart is undefined, 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 indexStart is equal to the value of indexEnd, 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
prev post

Table of Contents

  • What is Substring in JavaScript?
  • Get Substring in Javascript