How to Get the Last Character of String in Javascript?

In this blog post, we learn how to get the last character of string in javascript. To find the last character we have two easy methods. The first one is a slice() and another method is length properties.

Method 1: Slice()

The slice() the method extracts a section of a string and returns it as a new string, without modifying the original string. So here we find only the last character so we use -1 parameter for the slice function.

var str = 'codermen';
var ans = str.slice(-1);
console.log(ans);

Result is

n

Another example of slice()

var str = 'codermen';
var ans = str.slice(-2);
console.log(ans);

Result

en

Method 2: lenght proprties

The lenght property returns the length of a string.

const str = "codermen";
const stringLength = str.length; // string length is 8
console.log(str.charAt(stringLength - 1)); 

Result

n

Here, We can Get the Last Character of String in Javascript using slice() and lenght properties.

I hope its help you.