Get the last character of a string in JavaScript
📅December 25, 2022
Today, we will show you how to get the last character of a string in JavaScript. There are multiple methods to get the last character of a string.
Get the last character of a string in JavaScript
1. Using charAt() method
const str = 'abcde';
const last = str.charAt(str.length - 1);
console.log(last); // Output: e
2. Using slice() method
const str = 'abcde';
const last = str.slice(-1);
console.log(last); // Output: e
3. Using at() method
const str = 'abcde';
const last = str.at(-1);
console.log(last); // Output: e
4. Using split() & pop() method
const str = 'abcde';
const last = str.split('').pop();
console.log(last); // Output: e
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂