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.

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 of str.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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.