Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)

JavaScript Substring() – How is it Different from Substr() & slice()?

Last updated on Nov 21,2022 90.5K Views

A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.

Manipulating data is an integral part of any web application. JavaScript provides different ways for parsing and extracting data. The JavaScript Substring, Substr and Slice methods are used for extracting strings from a larger one. This JavaScript Substrig article will provide in-depth knowledge about the same in the following sequence:


What is a Substring?

A substring is a subset of another string. It is a contiguous sequence of characters within a string.

substring-javascript substring - edureka

For example, “Welcome to Edureka” is a substring of the string “Welcome to Edureka Online Courses“. But, “Edureka Courses” is not a substring as it is a subsequence or the generalization of the string.


How to get a JavaScript Substring?

The substring() is an inbuilt function in JavaScript which returns a part of the given string from start index to end index. The indexing starts from zero. It is used to return a portion of the string, starting at the specified index and extending for a given number of characters afterward.


string.substring(indexA, [indexB])


  • indexA(Starting Index) − Integer between 0 and one less than the length of the string.
  • indexB(Ending Index) − Integer between 0 and the length of the string. It is optional.

This substring method returns the new sub-string based on given parameters.

Let’s take an example and see how the substring() function works.


<title>JavaScript String substring() Method</title>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%20%3D%20%22text%2Fjavascript%22%3E%0Avar%20str%20%3D%20%22Welcome%20to%20Edureka%20JavaScript%20Training%22%3B%0Adocument.write(%22(3%2C7)%3A%20%22%20%2B%20str.substring(3%2C7))%3B%0Adocument.write(%22(0%2C10)%3A%20%22%20%2B%20str.substring(0%2C10))%3B%0Adocument.write(%22(11)%3A%20%22%20%2B%20str.substring(11))%3B%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />


(3,7): come
(0,10): Welcome to
(11): Edureka JavaScript Training


Using Substring with length Property

The length property is used to get the length of a string. The substring() method and length property are used together to extract the last characters of a particular string. This method is easier to remember as you don’t need to know the starting and ending indices.


var anyString = 'Edureka';
var anyString1 = anyString.substring(anyString.length - 5); //Displays last 5 characters



Now that you know what is substring and how it works, let’s move on with our JavaScript Substring article and discuss the differences between substring, substr, and slice.


Substring() vs Substr() vs Slice()

These methods are used to extract parts of a string and return the extracted parts in a new string. All of these do not change the original string.

JavaScript substring() method returns part of the string between startIndex & endIndex. It excludes the endIndex character or up to the end of the string.JavaScript substr() method returns part of the string starting from an index and number of characters after the start index. It has two parameters such as startIndex and Lengthslice() method is similar to substring. It returns partial-string between start index & end index. It excludes end index character or up to the end of the string if end index not provided

Substring() Syntax:

string.substring(startIndex[, endIndex])


var string = "Edureka JavaScript";
var substring = string.substring(0,7);



Substr() Syntax:

string.substr(startIndex[, length])


var string = "Edureka JavaScript";
var substr = string.substr(8,10);



Slice() Syntax:

string.slice(startIndex[, endIndex])


var string = "Edureka JavaScript";
var substr = string.slice(8,12);



With this, we have come to the end of our JavaScript Substring blog. I hope you understood how it is used to exact any part of a string.

Now that you know about JavaScript Substring, check out the Web Developer Course by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Got a question for us? Please mention it in the comments section of “JavaScript Substring” and we will get back to you.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 7th October,2023

7th October

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th November,2023

25th November

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

JavaScript Substring() – How is it Different from Substr() & slice()?