How to get the last character of a string in Javascript
- 0
How can I get the last character of a string dynamicaly using Javascript?
Answers
- 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);