JavaScript Comparison Between String.slice() Vs String.substr()
app development

JavaScript Comparison Between String.slice() Vs String.substr() Vs String.substring()

Slice, Substr, and Substring in JavaScript are differs in various aspects

https://dk2dyle8k4h9a.cloudfront.net/JavaScript Comparison Between String.slice() Vs String.substr() Vs String.substring()

Top Companies

01.
InMobi
Singapore
02.
Dot Com Infoway
New York, USA
03.
M&C Saatchi Mobile
New York City, USA
04.
Techmagnate
New Delhi, India
05.
Fetch
London, UK
View full report
Updated On
August 27, 2018 06:01 PM
3 min read

Latest Articles

With this article on JavaScript, we will be clearing out your confusion in using String.slice() vs String.substr() vs String.substring() together or separately.  But first, we will start by explaining the key differences in the JavaScript strings i.e. substr and substring.

JavaScript basically has two string methods that are:

  1. Substr
  2. Substring

The confusion mainly arises in these two JavaScript strings because they look very identical in the first glance, as both of these string methods also return a substring from a given string.

So, what separates these similar two string methods…

What tells apart these JavaScript string methods are their 'Second Parameters' because both of the numbers are expecting two different outcomes.

For example:

If we are using a 'substr' the second parameter is the number of the characters to include in the substring. Like -

var s = "string";

s.substr(1, 3); // would return 'tri'

 

var s = "another example";

s.substr(3, 7); // would return 'ther ex'

But when we are using a 'substring', then the second parameter is the first index that is not to include. Like -

var s = "string";

s.substring(1, 3); // would return 'tr'

 

var s = "another example";

s.substring(3, 7); // would return 'ther'

String.slice() vs String.substr() vs String.substring()

The methods slice(), substring(), and substr() are the methods that extract parts of a string and then return the extracted parts in a new string. Also, all of these methods do not change the original string from which they extract.

For the slice method i.e. slice(), it can take these two arguments:

Argument #1 - With 'begin' the position of where to begin the extraction is required. The position of the first character starts at 0 and it can use the negative values to specify the exact location from the end of the string.

Argument #2 - For 'end', the position where to end the extraction is optional to mention. Once omitted, the slice() will then select all the characters from the 'start' position to the end of the string and can also use the negative numbers to select from the end of the main string.

Example:

var numbers="0123456789";

console.log(numbers.slice(2,4));   // shows 23

console.log(numbers.slice(-7,-3)); // shows 3456

For the substring method i.e. substring(), it can take these two arguments:

Argument #1 - Starting with 'from' it is required to mention where to extraction from and the first character's index will be set as 0.

Argument #2 - Beginning with 'to', it is optional to mention where the end of extraction will be. Once omitted, it will extract all the rest of the string itself.

Example:

console.log(numbers.substring(-3,5));  // shows 01234

console.log(numbers.substring(NaN,5)); // shows 01234

console.log(numbers.substring(-3));    // shows 0123456789

For the substr method i.e. substr(), it can take these two arguments:

Argument #1 - With the 'start' the extraction will begin and the first character will begin at an index of 0. For extracting the characters from the end of the string, the negative start numbers will be used.

Argument #2 - For the 'length', the mentioning of the number of characters to extract is optional. And if is it omitted, the rest of the string will be extracted automatically.

Example:

console.log(numbers.substr(3)); // shows 3456789

console.log(numbers.substr(-3));// shows 789

Main difference between String.slice() and String.substring()

If the startIndex > endIndex (startIndex greater than endIndex) then the slice() will run an empty string but substring() will swap those parameters. And if startIndex < endIndex (endIndex greater than startIndex) then slice() will consider it as string.length-index whereas substring() will treat them as a zero i.e. 0.

Do let us know if you agree with the above mentioned differences between JavaScript slice(), substr() and substring(), by commenting down below.

Read More About:

Akash Singh Chauhan
Written By
Akash Singh Chauhan

Akash Singh Chauhan is a senior writer at MobileAppDaily and he mainly covers all the latest happenings and tweaks in mobile app technology. Being an Engineering graduate he is always compelled to the technology and tries to discover new trends in the tech world. Along with any tech news he also never misses a single episode of ‘Dragon Ball’.

app development

All About iOS 11 And iOS 12 Features That You Can't Miss

Vikram Khajuria 4 min read  

iOS 11 is ranked among the world’s most advanced mobile operating system. The OS can take any challenge head on and promises to rank in the top. Like every other update, iOS 11.4 has brought new features into the armory. The introduction of augmented reality and ClassKit support has taken iOS

app development

What Separates A Native App From A Hybrid App?

Vikram Khajuria 4 min read  

Due to the advancement in the tech, various kind of apps have made their way into the app industry and two of the top heavyweights are native and hybrid apps. The native app is the basic one whereas, hybrid one is the advanced version. Though the hybrid app has rightly followed the native app and ha

app development

Essential Factors to Check Before Developing a Campus App

James Lawson 4 min read  

The reality of today's time is that after a certain age, every student owns a mobile phone of his or her choice. Students spend hours on their smartphone whether to explore the latest social media or to check out the new camera feature. Apart from just used for the purpose of entertainment, ther

app development

Know The Best Way To Publish Your App On Google Play Store

Akash Singh Chauhan 4 min read  

After the success of the mobile app industry and exponential growth of mobile app economy, it is necessary to have an app for your business. However, once your mobile app development process gets completed and you have the package to deliver the users, what you will do next? Now you need a channel t