Menu Close

How to Get Last Character of String in Javascript?

Codeamend

This article goes in detailed on javascript get last character of string. you will learn get last character of string javascript. we will help you to give example of find last character of string javascript. follow bellow step for select last character of string javascript.

To get the last character of a string in JavaScript, you can use the charAt() method with the length of the string minus one as its argument. Here’s an example:

Example 1

const myString = "Hello World";

const lastChar = myString.charAt(myString.length - 1);

console.log(lastChar); 

// Output: d 

Alternatively, you can also use square brackets and the index of the last character to get it directly:

Example 2:

const myString = "Hello World";

const lastChar = myString[myString.length - 1];

console.log(lastChar);

// Output: d 

Both methods will give you the same result.

Posted in JavaScript

You can also read...