129

If I have the following variable in javascript

 var myString = "Test3";

what is the fastest way to parse out the "3" from this string that works in all browsers (back to IE6)

2
  • 5
    @Pekka - i disagree, there are 7 answers here and most of them are incorrect as they don't work in all browsers. Not as simple as it seems. Commented Sep 17, 2011 at 0:12
  • Here's a speed comparison of different methods. Usually either .substr( -1 ) or .splice( -1 ) wins, but even the usually slowest .split( ' ' ).pop( ) won one time for me. Commented Aug 26, 2020 at 12:30

7 Answers 7

213

Since in Javascript a string is a char array, you can access the last character by the length of the string.

var lastChar = myString[myString.length -1];
Sign up to request clarification or add additional context in comments.

9 Comments

Nice—and this is slightly faster. See jsperf.com/get-last-character-from-string
Note: this was introduced in es5, so not supported in very old browsers, eg. IE6.
IE6 - 23 Feb 2017 - seriously?
It's not going to work in Mosaic either, just an FYI ;) To be fair, the question does specify they are looking for IE6 compatibility, so a note to avoid any confusion for any JavaScript archaeologists out there is perhaps apt.
@KhaledRamadan It seems like the answer in the linked question is fine. Is there something I can help you with?
|
114

It does it:

myString.substr(-1);

This returns a substring of myString starting at one character from the end: the last character.

This also works:

myString.charAt(myString.length-1);

And this too:

myString.slice(-1);

5 Comments

substr doesn't seem to be supported in Internet Explorer
do you agree that this doesn't work in IE6 7?
@leora IE can't handle negative values in substr according to developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
Better slice. As String.prototype.substr(…) is not strictly deprecated (as in "removed from the Web standards"), it is considered a legacy function and should be avoided when possible. It is not part of the core JavaScript language and may be removed in the future. If at all possible, use the substring() method instead. - From MDN
Here's a speed comparison of different methods. Usually either .substr( -1 ) or .splice( -1 ) wins, but even the usually slowest .split( ' ' ).pop( ) won one time for me.
6
 var myString = "Test3";
 alert(myString[myString.length-1])

here is a simple fiddle

http://jsfiddle.net/MZEqD/

1 Comment

This is the only answer that utilizes the fact that a string is an array of characters
6

Javascript strings have a length property that will tell you the length of the string.

Then all you have to do is use the substr() function to get the last character:

var myString = "Test3";
var lastChar = myString.substr(myString.length - 1);

edit: yes, or use the array notation as the other posts before me have done.

Lots of String functions explained here

Comments

3
myString.substring(str.length,str.length-1)

You should be able to do something like the above - which will get the last character

3 Comments

I had to get last 10 characters and it worked for me myString.substring(str.length,str.length-10). Thanks buddy!
@hmd I think you mean: myString.substring(str.length-10,str.length)
@DanSalo myString.substring(myString.length,myString.length-10) or myString.substring(myString.length-10, myString.length) both returns the same results. Last 10 characters.
2

Use the charAt method. This function accepts one argument: The index of the character.

var lastCHar = myString.charAt(myString.length-1);

Comments

1

You should look at charAt function and take length of the string.

var b = 'I am a JavaScript hacker.';
console.log(b.charAt(b.length-1));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.