Solution
To get the last letter of a string in react js, use the string index
method it means when you use string[string.length - 1]
it will return the last letter of the string.
Snippet
In this snippet section, we will see an example of getting the last letter of a string program sample.
const str = "ReactJsSnippet";
console.log(str[str.length - 1]) // t
Example
In this example, we will get and show the last letter of the string in the console and page.
Let’s start coding…
App.js
import { useState, useEffect } from "react";
export default function App() {
const [str, setStr] = useState('ReactJsSnippet');
useEffect(() => {
console.log(str[str.length - 1])
}, []);
return (
<div>
<p>{str[str.length - 1]}</p>
</div>
);
}
Output