Hi đź‘‹,

Welcome To Infinitbility! ❤️

To get the first character of the string to react js, just use charAt(0) it will return the first char of a string.

For the sample let’s see how can we use charAt(0) on a string.

// 👇️ i
console.log("infinitbility".charAt(0));

Today, I’m going to show you How do I get the first character of a string in to react js, as above mentioned here, I’m going to use the charAt(0) so we can first alphabet of the string.

Let’s start today’s tutorial how do you get the first character of a string in react js?

In this example, we will do

  1. create a sample string state
  2. use charAt(0) method to get first char
  3. print the first character on a page

Let’s write code…

import React, { useState, useEffect } from "react";
import "./styles.css";
export default function App() {
  const [str, setStr] = useState("infinitbility.com");


  return (
    <div className="App">
      <h1>{str.charAt(0)}</h1>
    </div>
  );
}

As mentioned above, we are taken the example of a sample string, used a char at a function to get the first char, and printed the output on a screen.

Let’s check the output.

I hope it’s help you, All the best 👍.