How to Get the Last Character or Last N Characters of a String in JavaScript
Extracting characters from the end of a string is a common task in programming. You might need to get the last character to check for a closing parenthesis, or get the last few characters to find a file extension. Modern JavaScript provides several simple and highly readable methods for these tasks, with slice() and at() being the most effective.
This guide will teach you the best methods for getting the last character and the last N characters from a string, explaining the use case for each and why they are superior to older approaches.
How to Get the Last Character of a String
There are two excellent, modern methods for this.
The Modern Method (Recommended): string.at(-1)
The String.prototype.at() method is the newest and most readable way to get a character at a specific index. Its key feature is that it accepts negative integers, which count back from the end of the string.
Problem: you need to get the last character of a string.
// Problem: How to get the character 'd'?
const myString = 'Hello, World';
Solution:
const myString = 'Hello, World';
const lastChar = myString.at(-1);
console.log(lastChar); // Output: "d"
This is the recommended best practice because its intent is perfectly clear. If the index is out of bounds (e.g., on an empty string), at() safely returns undefined.
The Classic Method: string.slice(-1)
The String.prototype.slice() method is also extremely effective. When passed a negative index, it starts the "slice" from the end of the string.
Solution:
const myString = 'Hello, World';
const lastChar = myString.slice(-1);
console.log(lastChar); // Output: "d"
This is also an excellent and highly readable choice. If the string is empty, slice(-1) safely returns an empty string ''.
How to Get the Last N Characters of a String
The slice() method is the definitive tool for this task. By passing a negative number, you tell it how many characters to extract from the end of the string.
Problem: you need to get the last 3 characters of a string.
const filename = 'document.pdf';
Solution:
const filename = 'document.pdf';
// Get the last 4 characters to find the extension
const extension = filename.slice(-4);
console.log(extension); // Output: ".pdf"
// Get the last 3 characters
const lastThree = filename.slice(-3);
console.log(lastThree); // Output: "pdf"
If you provide a number larger than the string's length, slice() will safely return the entire string.
Older, Less Common Methods
You may see these methods in older codebases, but they are generally not recommended for new code.
-
Bracket Notation:
myString[myString.length - 1]- This works, but it's verbose and less readable than
at(-1). It also returnsundefinedfor an empty string.
- This works, but it's verbose and less readable than
-
charAt():myString.charAt(myString.length - 1)- This is also verbose. It returns an empty string
''for an empty string, which can sometimes be preferable toundefined.
- This is also verbose. It returns an empty string
-
substring():myString.substring(myString.length - 1)- This works but is less intuitive than
slice()becausesubstring()does not accept negative arguments.
- This works but is less intuitive than
The modern at() and slice() methods are superior in almost every way.
Conclusion
For getting characters from the end of a string, modern JavaScript provides clean and highly readable tools.
- To get the single last character, the
string.at(-1)method is the recommended best practice for its clarity.string.slice(-1)is an excellent alternative. - To get the last N characters, the definitive method is
string.slice(-N).
By using these modern, negative-indexing methods, you can write concise and expressive code for any string manipulation task.