When you write JavaScript, you need to know what string manipulation methods/functions are available. In the previous posts  JavaScript substr Function and JavaScript substring Function, we gave examples on usages of the two most common javascript string functions. You can also find the details below:

JavaScript substring() Method

JavaScript substring is used to take a part of a string. The syntax of JavaScript substring method is given below:



Notes about Javascript substring:


substring Example:

<script type="text/javascript">

var str = "Hello World";


The output of the above code is:

o wo

JavaScript substr() Method

The JavaScript substr() method works slightly different. Instead of the second parameter being an index number, it gives the number of characters. The syntax of JavaScript substr() is given below:



Notes about substr:


JavaScript substr Example:

<script type="text/javascript">

var str = "Hello World";


The output of the above code is:

o wo

Since JavaScript substr() is not cross-browser, I never use it.

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 4.0 by 17 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


November 21. 2007 15:33

I knew in the back of my head there was a difference.  I'm guilty of using both though... now I know the difference.  I found value to this post, thanks for taking the time.

Casey Wise | Reply

November 21. 2007 17:11

substr is cross browser, *today*,
The rulers today are: IE6, FF 2, Safari 2, Opera 8.5 and they support substr..
you can use substr as much as you want.

These browser's versions that you mentioned are not used anymore by anyone

Nir Tayeb | Reply

November 21. 2007 17:20

You are right, Nir.

However, if we talk about cross-browser javascript, it is important to mention the browser versions that are supported.

Blogging Developer | Reply

December 29. 2007 23:27

Yeah, that is a way overboard interpretation of "cross-browser."  The phrase does not mean "backward-compatible;" that is an impossibility these days.  At the very earliest, IE 5.5 could try to be supported, but anything before that is obsolete.  Otherwise, we'd be stuck without things like DOM functions or XMLHttpRequest.

Kyle | Reply

July 11. 2008 07:34

this example was very benificial and solved problem of mine..
actully on many sites stbstring () function is given not substr() i want that .. so it was really good

pankaj | Reply

January 7. 2009 13:40

The output (o wo) for both examples is wrong, it should be:
o Wo

Otototot | Reply

February 5. 2009 13:37

Great article!  I thought I was doomed to .substring() forever but you saved the day ;)  Thanks!

Norio | Reply

February 20. 2009 22:32

Note also that 'length' must be an unsigned integer. Negative numbers will not start at the end of the string as one might expect.

orrin McCormack | Reply

March 11. 2009 17:41

you are voted!
track back from webdevvote.com

webdevvote.com | Reply

October 15. 2009 10:27

Thx. This's useful, as at first glance the developer may think there is no difference.

WebDev | Reply

October 20. 2009 16:37

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: JavaScript substring vs. substr - The Difference Between JavaScript String Extraction Functions - http://bit.ly/tlmyD

uberVU - social comments | Reply

October 30. 2009 09:37

I just hope to have understood this the way it was meant

cash loans | Reply

December 26. 2009 19:32

Just try to smile for about 2-3 mins then you can get back to work

loans | Reply

March 15. 2010 14:58

The man who will use his skill and constructive imagination to see how much he can give for a dollar, instead of how little he can give for a dollar, is bound to succeed

cash loans | Reply

Add comment

(Will not be displayed!)