In this article, we’ll be looking at some ways to quickly get the last character of a string in JavaScript.
Subscribe to Coding Beauty Newsletter
Gain useful insights and advance your web development knowledge with weekly tips and tutorials from Coding Beauty. Over 2,000 developers subscribe.
1. String at() Method
The get the last character of a string, we can call the at() method on the string, passing -1 as an argument. For example, str.at(-1) returns a new string containing the last character of const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 0.
The const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 1 at() method returns the character of a string at the specified index. When negative integers are passed to at(), it counts back from the last string character.
2. String charAt() Method
Alternatively, to get the last character of a string, we can call the const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 method on the string, passing the last character index as an argument. For example, const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 5 returns a new string containing the last character of const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 0.
const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // kThe const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 1 const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 method takes an index and returns the character of the string at that index.
Tip
In JavaScript, arrays use zero-based indexing. This means that the first character has an index of const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 9, and the last character has an index of const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 0.
Note
If we pass an index to const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 that doesn’t exist on the string, it returns an empty string (const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 2):
const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // ''3. Bracket Notation ([]) Property Access
We can also use the bracket notation (const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 3) to access the last character of a string. Just like with the const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 method we use const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 0 as an index to access the last character.
const str = 'book'; const lastCh = str[str.length - 1]; console.log(lastCh); // kNote
Unlike with const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4, using the bracket notation to access a character at a non-existent index in the string will return const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 7:
4. String split() and Array pop() Methods
With this method, we call the const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 8 method on the string to get an array of characters, then we call const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 9 on this array to get the last character of the string.
const str = 'book'; const lastCh = str.split('').pop(); console.log(lastCh); // kWe passed an empty string (const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 2) to the const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 8 method to split the string into an array of all its characters.
const str = 'book'; console.log(str.split('')); // [ 'b', 'o', 'o', 'k' ]The Array const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 9 method removes the last element from an array and returns that element. We call it on the array of characters to get the last character.
Every Crazy Thing JavaScript Does
A captivating guide to the subtle caveats and lesser-known parts of JavaScript.
Sign up and receive a free copy immediately.
Ayibatari Ibaba
Ayibatari Ibaba is a software developer with years of experience building websites and apps. He has written extensively on a wide range of programming topics and has created dozens of apps and open-source libraries.