How to get the last character of a string in Javascript

Question

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

Answer ( 1 )

    0
    2023-01-26T15:50:36+00:00

    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);

Leave an answer