To get the last character of a string in JavaScript, you can use the indexing operator with the length of the string minus one.
In this example,
str
with a value of "Hello World"
.[]
with str
and str.length - 1
to get the last character.let str = "Hello World";
let lastChar = str[str.length - 1];
console.log(`Last character of the string is: ${lastChar}`);
Last character of the string is: d
In this example,
str
with a value of "apple"
.[]
with str
and str.length - 1
to get the last character.let str = "apple";
let lastChar = str[str.length - 1];
console.log(`Last character of the string is: ${lastChar}`);
Last character of the string is: e
In this tutorial, we learned How to get Last Character in String in JavaScript language with well detailed examples.