How To Get The Last Character of a String in JavaScript

Reading Time: 5 minutes
1,008 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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more