Solution
To get the first letter of a string in react js, use the string index
method it means when you use string[0]
it will return the first letter of the string.
Snippet
In this snippet section, we will see an example of getting the first letter of a string program sample.
const str = "ReactJsSnippet";
console.log(str[0]) // R
Example
In this example, we will get and show the first 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[0])
}, []);
return (
<div>
<p>{str[0]}</p>
</div>
);
}
Output