In this quick tip, you’ll see how to get last character of a string using JavaScript. Most of the time, to work on strings you need to convert string to an array and then access string characters.
To Get Last Character Of String
The following code converts string to an array:
let str = "CodeHandbook";
let arr = str.split('');
console.log(arr);
Basically, the above code splits the string based on space and converts it into an array.
Now you can access any nth character of an array using arr[n]. To get the last character of an array the code would be:
let str = "CodeHandbook";
let arr = str.split('');
let last_character = arr[arr.length - 1];
console.log('Last character is ', last_character);
So, in this quick tutorial you learnt how to get nth character or last character of an array using JavaScript.