Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the last character in a string? [duplicate]

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)

like image 555
leora Avatar asked Oct 10 '22 22:10

leora


1 Answers

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];
like image 209
Jamie Dixon Avatar answered Oct 13 '22 12:10

Jamie Dixon