- How to get the last character of a string in JavaScript
- You might also like.
- Javascript get last characters
- # Get the last Character of a String in JavaScript
- # Get the last Character of a String using indexing
- # Get the last Character of a String using slice()
- # Get the last character of a String using String.at()
- # Additional Resources
- How to Get the Last Character of a String in JavaScript
- 1. String at() Method
- 2. String charAt() Method
- Tip
- Note
- 3. Bracket Notation ([]) Property Access
- Note
- 4. String split() and Array pop() Methods
- 11 Amazing New JavaScript Features in ES13
- How to Get the Last Characters of a String
- Strings
How to get the last character of a string in JavaScript
There are multiple ways to get the last character of a string in JavaScript. You can use the charAt() , slice() , substring() , at() , or bracket notation property access to get the last character in a string.
To get the last character of a string, you can call the charAt() method on the string, passing it the last character index as a parameter. This method returns a new string containing the character at the given index.
const str = 'JavaScript' const lastChar = str.charAt(str.length - 1) console.log(lastChar) // t
The charAt() method returns the character of a string at the specified index. An empty string is returned if the given index does not exist:
const str = 'JavaScript' const lastChar = str.charAt(20) console.log(lastChar) // ""
Indexes are zero-based in JavaScript. The first character of a string has an index of 0 , and the last has an index of str.length — 1 .
const str = 'JavaScript' const lastChar = str[str.length - 1] console.log(lastChar) // t
Unlike the charAt() method, the bracket notation returns undefined if the given index does not exist:
const str = 'JavaScript' const lastChar = str[20] console.log(lastChar) // undefined
To get the last character of the string, call the substring() on the string, and the last character index as a start index:
const str = 'JavaScript' const lastChar = str.substring(str.length - 1) console.log(lastChar) // t
The substring() method extracts characters between the start and end indexes from a string and returns the substring.
To get the last character of the string, call the slice() method on the string, passing -1 as a negative start index:
const str = 'JavaScript' const lastChar = str.slice(-1) console.log(lastChar) // t
The slice() method extracts a part of a string between the start and end indexes, specified as first and second parameters. It returns the extracted part as a new string and does not change the original string. The slice() method also accepts a negative start index to slice the string from the end. You can even use this approach to get the last N characters of a string:
const str = 'JavaScript' const last2 = str.slice(-2) console.log(last2) // pt const last4 = str.slice(-4) console.log(last4) // ript const last6 = str.slice(-6) console.log(last6) // Script
The at() method takes an integer as input and returns the character of a string at the specified index. To get the last character of the string, call the at() method on the string with a -1 index:
const str = 'JavaScript' const lastChar = str.at(-1) console.log(lastChar) // t
When negative integers are passed to at() , it counts back from the last string character. The at() method returns undefined if the index does not exist:
console.log(str.at(20)) // undefined
The at() method is a new addition to JavaScript and only works in modern browsers. ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.
You might also like.
Javascript get last characters
Last updated: Dec 21, 2022
Reading time · 3 min
# Get the last Character of a String in JavaScript
To get the last character of a string, call the charAt() method on the string, passing it the last index as a parameter.
For example, str.charAt(str.length — 1) returns a new string containing the last character of the string.
Copied!const str = 'abcde'; // ✅ Get the last character of a string using charAt() const last = str.charAt(str.length - 1); console.log(last); // 👉️ e // ✅ Get the last character of a string using slice() const lst = str.slice(-1); console.log(lst); // 👉 'e' const lst2 = str.slice(-2); console.log(lst2); // 👉️ 'de' // ✅ Get the last character of a string using String.at() const last_ = str.at(-1); console.log(last_); // 👉️ e
The argument we passed to the String.charAt method is the index .
JavaScript indexes are zero-based, so the first character in the string has an index of 0 and the last character in the string has an index of str.length — 1 .
If passed an index that doesn’t exist, the charAt() method returns an empty string.
Copied!const str = ''; const last = str.charAt(str.length - 1); console.log(last); // 👉️ ""
# Get the last Character of a String using indexing
An alternative approach is to directly access the string at the last index.
Indexes are zero-based, so the index of the last character in the string is str.length — 1 .
Copied!const str = 'abcde'; const last = str[str.length - 1]; console.log(last); // 👉️ e
Accessing the character at the last index achieves the same result as using the charAt() method.
However, if we try to access a character at an index that doesn’t exist, we get undefined back.
Copied!const str = ''; const last = str[str.length - 1]; console.log(last); // 👉️ undefined
This is the main reason I prefer the charAt() method, which returns an empty string when supplied with a non-existent index.
It’s always easier to reason about and manage an application if we’re consistent with types.
# Get the last Character of a String using slice()
Alternatively, you can use the String.slice() method.
When passed an index of -1 , the slice() method returns the last character of the string.
Copied!const str = 'abcde'; const last = str.slice(-1); console.log(last); // 👉 'e'
The parameter we passed to the String.slice method is the start index.
The slice() method can be passed a negative start index to count backward.
When passed an index of -1 , the slice method returns the last character of the string.
Copied!const str = 'abcde'; const last = str.slice(-1); console.log(last); // 👉 'e'
The slice() method returns an empty string if the specified index is not found in the string.
Copied!const str = ''; const last = str.slice(-1); console.log(last); // 👉 ''
# Get the last character of a String using String.at()
You can also use the String.at() method.
The String.at() method can be passed a value of -1 to return the last character of a string as it supports negative indexing.
Copied!const str = 'bobbyhadz'; const last = str.at(-1); console.log(last); // 👉️ z
We used the String.at method to get the last characters of a string.
The method takes an integer that represents the index and returns the character at the specified index.
The method supports negative integers to count backward. For example, -1 returns the last character in the string and -2 returns the second last character.
Copied!const str = 'bobbyhadz'; const last = str.at(-1); console.log(last); // 👉️ z console.log(str.at(-2)); // 👉️ d console.log(str.at(-3)); // 👉️ a
We passed a value of -1 to the String.at() method to get the last character in the string.
If you pass an index that is out of range to the String.at() method, the method returns undefined .
Copied!const str = ''; console.log(str.at(-1)); // 👉️ undefined
Which approach you pick is a matter of personal preference. I’d use the String.slice() method because it returns an empty string when the supplied index is out of range.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
How to Get the Last Character of a String in JavaScript
In this article, we’ll be looking at some ways to quickly get the last character of a string in JavaScript.
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 str .
const str = 'Coding Beauty'; const lastChar = str.at(-1); console.log(lastChar); // y
The String 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 charAt() method on the string, passing the last character index as an argument. For example, str.charAt(str.length — 1) returns a new string containing the last character of str .
const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k
The String charAt() 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 0 , and the last character has an index of str.length — 1 .
Note
If we pass an index to charAt() that doesn’t exist on the string, it returns an empty string ( » ):
const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // ''
3. Bracket Notation ([]) Property Access
We can also use the bracket notation ( [] ) to access the last character of a string. Just like with the charAt() method we use str.length — 1 as an index to access the last character.
const str = 'book'; const lastCh = str[str.length - 1]; console.log(lastCh); // k
Note
Unlike with charAt() , using the bracket notation to access a character at a non-existent index in the string will return undefined :
const str = 'book'; const lastCh = str[10]; console.log(lastCh); // undefined
4. String split() and Array pop() Methods
With this method, we call the split() method on the string to get an array of characters, then we call pop() on this array to get the last character of the string.
const str = 'book'; const lastCh = str.split('').pop(); console.log(lastCh); // k
We passed an empty string ( » ) to the split() 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 pop() 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.
11 Amazing New JavaScript Features in ES13
This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code.
How to Get the Last Characters of a String
There are several methods used to get the last character of a string. In this tutorial you can find the simple solution for your case.
You can use the Javascript string method .substr() combined with the length property.
Javascript substr method
let lastSeven = id.substr(id.length — 7); // «String1» console.log(lastSeven); let lastChar = id.substr(id.length — 1); // «1» console.log(lastChar);
The substr() method returns a section of the string, starting at the specified index and continuing to a given number of characters afterward.
Another method is .slice() which is used to get the last characters of the string:
Javascript slice method
The .slice() method takes out parts of a string and returns the extracted parts in a new string. This method is a better method to use for getting the last characters as it provides cross-browser compatibility.
Strings
The strings are used to store and manipulate text in JavaScript. There is no any separate type for a single character. The string’s internal format is always UTF-16. A string represents zero or more characters that are written inside quotes. The length property is used to find the length of a string.
Typing the property correctly is very important. If you mistype it by calling str.length() instead of just str.length, it will not work.