How To Get The Last Character of a String in JavaScript

Share this Article
Reading Time: 5 minutes
406 Views

Inside this article we will see the concept i.e How To Get The Last Character of a String in JavaScript Tutorial. Article contains the classified information about getting last character from string using javascript.

If you are looking for a solution i.e javascript get last character of string then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Here, we will use three different methods of javascript by which we can get last character of a string. You will learn in detail about each – charAt(), slice(), Bracket notation.

Learn More –

Let’s get started.

Example #1: Get Last Character of String Using charAt() Method

Here,

We will use the concept of Javascript i.e How to Get the Last Characters of a String.

Indexes are zero-based in JavaScript. The first character in the string has an index of 0, so the last character in the string has an index of str.length – 1.

//..

const str = 'OnlineWebTutor';

const last = str.charAt(str.length - 1);

console.log(last); // r

//...

Console Output

Example #2: Get Last Character of String Using slice() Method

Here,

We will use the concept of Javascript i.e How to Get the Last Characters of a String.

In Js you can use the slice() method to get the last character of a string. When passed an index of -1, the slice() method returns the last character of the string.

//...

const str = 'OnlineWebTutor';

const last = str.slice(-1);
console.log(last); // 'r'

const last2 = str.slice(-2);
console.log(last2); // 'or'

const last3 = str.slice(-3);
console.log(last3); // 'tor'

//...

Console Output

The slice() method returns an empty string if the specified index is not found in the string.

Example #3: Get Last Character of String Using Bracket Notation

Here,

We will use the concept of Javascript i.e How to Get the Last Characters of a String.

To get the last character of a string, use bracket notation to 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.

//...

const str = 'OnlineWebTutor';

const last = str[str.length - 1];

console.log(last); // r

//...

Console Output

We hope this article helped you to learn How To Get The Last Character of a String in JavaScript Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5