by @saigowthamr

How to get last character of string in JavaScript

Home » javascript » How to get last character of string in JavaScript

There are two ways to get the last character of a string in JavaScript.

1. Using charAt() method

By passing string.length-1 as an argument to the charAt() method we can get the last character of a string.

const string = "hello";

const lastCharacter = string.charAt(string.length-1);

console.log(lastCharacter); // "o"

2. Using slice() method

If we pass -1 as an argument to the slice() method we can get the last character of a string.

const string = "hello";

const lastCharacter = string.slice(-1);

console.log(lastCharacter); // "o"