JavaScript - How To Get The Last Caracter of a String?
Here are the various approaches to get the last character of a String using JavaScript.
1. Using charAt() Method (Most Common)
The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.
const s = "JavaScript";
const lChar = s.charAt(s.length - 1);
console.log(lChar);
Output
t
- s.length - 1 calculates the index of the last character.
- s.charAt(index) retrieves the character at the specified index.
2. Using slice() Method
The slice() method extracts a portion of the string. By passing -1 as an argument, you can extract the last character.
const s = "JavaScript";
const lChar = s.slice(-1);
console.log(lChar);
Output
t
- A negative index in slice() counts from the end of the string.
- s.slice(-1) extracts the last character.
3. Using substr() Method
The substr() method extracts a substring starting from a specified position. Although deprecated, it's still supported in most environments.
const s = "JavaScript";
const lChar = s.substr(s.length - 1, 1);
console.log(lChar);
Output
t
- The first parameter specifies the starting index.
- The second parameter specifies the number of characters to extract.
4. Using String Length for Index
You can directly use the length property of a string to access the last character by treating the string as an array-like object.
const s = "JavaScript";
const lChar = s[s.length - 1];
console.log(lChar);
Output
t
- Strings are array-like, so you can use bracket notation to access individual characters.
- s[s.length - 1] directly retrieves the last character.
5. Using String.at() Method
The at() method retrieves a character at a specified index, including support for negative indices.
const s = "JavaScript";
const lChar = s.at(-1);
console.log(lChar);
Output
t
- s.at(-1) retrieves the last character using a negative index, making it a convenient and modern approach.
6. Using match() Method with Regular Expressions
The match() method combined with a regular expression can extract the last character of a string.
const s = "JavaScript";
const lChar = s.match(/.$/)[0];
console.log(lChar);
Output
t
- /.$/ is a regex pattern that matches the last character of the string (. matches any character, and $ asserts the end of the string).
- match() returns an array of matches, and [0] retrieves the matched character.
7. Using Array.prototype.pop() Method
By splitting the string into an array, you can use the pop() method to retrieve and remove the last character.
const s = "JavaScript";
const lChar = s.split("").pop();
console.log(lChar);
Output
t
- s.split("") splits the string into an array of characters.
- pop() removes and returns the last element of the array.
Comparison of Methods
Method | Use Case | Complexity |
---|---|---|
charAt() | Simple and widely supported, works well for most cases. | O(1) |
slice() | Ideal for clean extraction using negative indices. | O(n) (creates substring) |
subStr() | Deprecated, but still functional in most environments. | O(n) |
String Length | Most direct and efficient way using array-like indexing. | O(1) |
at() | Modern, clean approach with support for negative indices. | O(1) |
match() | Useful for advanced pattern matching scenarios. | O(n) |
pop( | Requires splitting the string, suitable for array logic. | O(n) |