How to get the last character of a string in Javascript

Asked by: tomlance79
Date:
Viewed: 376
Answers: 1
  • 0

How can I get the last character of a string dynamicaly using Javascript?

Answers

Answer by: jenniryan

Answered on: 21 Jul 2023

  • 0

You can get the last character of a string by decrementing the string length by 1.

The length property gives you the ability to determine the number of elements in an array, or the number of characters in a string.

For example:

const dog = "Rottweiler";
const length = dog.length;
console.log(length);

To get the last letter or character dynamically, you can do this since the string has an index of “0”:

const lastChar = dog[dog.length - 1];
console.log(lastChar);

 

Please log in to post an answer!