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.
Get the last character of a string using charAt()
method
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 ofstr.length - 1
.
Get the last character of a string using bracket notation
You can also use the bracket notation ([]
) to get the last character of a string:
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
Get the last character of a string using substring()
method
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.
Get the last character of a string using slice()
method
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
Get the last character of a string using at()
method
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.