Get the last character of a string in JavaScript
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
1 2 3 4 | const str = 'abcde'; const last = str.charAt(str.length - 1); console.log(last); // Output: e |
2. Using slice() method
1 2 3 4 | const str = 'abcde'; const last = str.slice(-1); console.log(last); // Output: e |
3. Using at() method
1 2 3 4 | const str = 'abcde'; const last = str.at(-1); console.log(last); // Output: e |
4. Using split() & pop() method
1 2 3 4 | const str = 'abcde'; const last = str.split('').pop(); console.log(last); // Output: e |
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂