get the last character of a string in javascript

You can use indexing to get the last character of a string in JavaScript. Here's an example:

index.tsx
let str = "hello";
let lastChar = str[str.length - 1];
console.log(lastChar); // "o"
85 chars
4 lines

In the above code, we first declare a string str with the value "hello". We then use the length property to get the length of the string, and subtract 1 to get the index of the last character. Finally, we use this index to get the last character of the string and store it in the variable lastChar.

Note that in JavaScript, you can also use the charAt() method to get the character at a specific index of a string. For example, to get the last character of the string str, you could also write:

index.tsx
let lastChar = str.charAt(str.length - 1);
43 chars
2 lines

gistlibby LogSnag