128

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)

peterh
  • 11,875
  • 18
  • 85
  • 108
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • 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. – leora Sep 17 '11 at 00:12
  • Here's a [speed comparison](https://jsben.ch/BhZiB) of different methods. Usually either `.substr( -1 )` or `.splice( -1 )` wins, but even the usually slowest `.split( ' ' ).pop( )` won one time for me. – s3c Aug 26 '20 at 12:30

7 Answers7

212

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];
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • 3
    Nice—and this is slightly faster. See http://jsperf.com/get-last-character-from-string – ryanve Jan 02 '12 at 01:03
  • 5
    Note: this was introduced in es5, so not supported in very old browsers, eg. **IE6**. –  Feb 23 '17 at 18:36
  • 73
    **IE6** - 23 Feb 2017 - seriously? – Avatar Nov 22 '17 at 08:50
  • 11
    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. – ChrisM Feb 19 '19 at 15:14
  • what is the string contains spaces? When I use trim I got undefined @Jamie Dixon – Khaled Ramadan Jun 10 '19 at 09:15
  • @KhaledRamadan Can you provide a more explicit example that demonstrates the problem you're having please? – Jamie Dixon Jun 10 '19 at 09:41
  • @JamieDixon Checkout the following please https://stackoverflow.com/questions/56523872/getting-the-last-character-of-a-string-is-not-working-after-trim – Khaled Ramadan Jun 10 '19 at 09:41
  • 2
    @KhaledRamadan It seems like the answer in the linked question is fine. Is there something I can help you with? – Jamie Dixon Jun 11 '19 at 12:22
  • @JamieDixon Nop Thank you :) – Khaled Ramadan Jun 13 '19 at 14:54
  • Here's a [speed comparison](https://jsben.ch/BhZiB) of different methods. Usually either `.substr( -1 )` or `.splice( -1 )` wins, but even the usually slowest `.split( ' ' ).pop( )` won one time for me. – s3c Aug 26 '20 at 12:30
113

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);
jo_va
  • 13,504
  • 3
  • 23
  • 47
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • 4
    substr doesn't seem to be supported in Internet Explorer – leora Sep 17 '11 at 00:04
  • 1
    do you agree that this doesn't work in IE6 7? – leora Oct 15 '11 at 13:39
  • @leora IE can't handle negative values in `substr` according to https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr – ryanve Jan 02 '12 at 01:05
  • 2
    **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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) – Ajeet Shah May 30 '20 at 21:52
  • Here's a [speed comparison](https://jsben.ch/BhZiB) of different methods. Usually either `.substr( -1 )` or `.splice( -1 )` wins, but even the usually slowest `.split( ' ' ).pop( )` won one time for me. – s3c Aug 26 '20 at 12:30
6
 var myString = "Test3";
 alert(myString[myString.length-1])

here is a simple fiddle

http://jsfiddle.net/MZEqD/

John Hartsock
  • 85,422
  • 23
  • 131
  • 146
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

Neoheurist
  • 3,183
  • 6
  • 37
  • 55
Joe
  • 4,852
  • 10
  • 63
  • 82
3
myString.substring(str.length,str.length-1)

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

diagonalbatman
  • 17,340
  • 3
  • 31
  • 31
  • 1
    I had to get last `10` characters and it worked for me `myString.substring(str.length,str.length-10)`. Thanks buddy! – hmd Jan 22 '18 at 18:47
  • @hmd I think you mean: `myString.substring(str.length-10,str.length)` – Dan Salo Feb 20 '19 at 18:54
  • @DanSalo `myString.substring(myString.length,myString.length-10)` or `myString.substring(myString.length-10, myString.length)` both returns the same results. Last 10 characters. – hmd Feb 25 '19 at 11:58
2

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

var lastCHar = myString.charAt(myString.length-1);
Rob W
  • 341,306
  • 83
  • 791
  • 678
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));
hmert
  • 101
  • 7