Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get last character of string in javascript, use substring() method to extracts last character from string. You have to only pass starting and ending postion of string so for last character pass text.length - 1 as parameter it will return last character of string.

Let’s see short example to use substring() to get last character from string.

text.substring(text.length - 1);

Today, I’m going to show you How do I get last character of string in javascript, as above mentioned, I’m going to use the above-mentioned substring() method.

Let’s start today’s tutorial how do you get last character of string in javascript?

Javascript get last character of string example using substring()

Here, we will take string variable with some data and use substring() method to get last char of string.

let str = "Infinitbility";

let char = str.substring(str.length - 1);
console.log(char);

// Output
// 'y'

Javascript get last character of string example using index

Here, we will take string variable with some data and use index method to get last char of string.

let str = "aGuideHub";

let char = str[str.length - 1];
console.log(char);

// Output
// 'b'

Javascript get last character of string example using charAt()

Here, we will take string variable with some data and use charAt() method to get last char of string.

let str = "SortoutCode";

let char = str.charAt(str.length - 1);;
console.log(char);

// Output
// 'e'

I hope it helps you, All the best đź‘Ť.