Tutorial

substring vs substr in JavaScript

Published on November 6, 2017
Default avatar

By Alligator.io

substring vs substr in JavaScript

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Similar to how the difference between the slice vs splice array methods can be hard to remember, it can also be hard to remember the difference between the substring and substr JavaScript string methods. Here’s a quick reference to help out with that.

TL;DR: substring takes a starting index and an end index while substr takes a starting index and a length of characters.

String.prototype.substring

The substring() method, all spelled out, returns a new string with a subset of the string. With one argument passed-in, we get the string starting from the specified index (inclusive) until the end of the string:

const myStr = 'Alligator';

const myNewStr = myStr.substring(2);

console.log(myNewStr); // ligator

With two arguments passed-in, we get a subset of the string from the starting index to the end index (exclusive):

const myStr = 'Alligator';

const myNewStr = myStr.substring(0, 3);

console.log(myNewStr); // All

String.prototype.substr

The substr() method is very similar, but the second argument is not for the end index, it’s for the amount of characters.

Here we want a 3-character string from a starting index of 2:

const myStr = 'Alligator';

const myNewStr = myStr.substr(2, 3);

console.log(myNewStr); // lig

Negative start index

Additionally, the first argument to substr can be a negative integer, in which case the start of the returned string is counted from the end of the string that the method is used on:

const myStr = 'Alligator';

const myNewStr = myStr.substr(-2);

console.log(myNewStr); // or

Same Result When Only One Argument

When only the first argument is used and is a positive integer, both substring and substr return the same value:

const myStr = 'Alligator';

const myNewStrViaSubstring = myStr.substring(3);

const myNewStrViaSubstr = myStr.substr(3);

console.log(myNewStrViaSubstring); // igator

console.log(myNewStrViaSubstr); // igator

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel