Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To each character of string in react js, use for...of loop, it will return each character one by one. You have to just use for...of loop on your string string.

Let’s see short example to use for loop on string to get each char.

for (let char of string) {
  console.log(char)
}

Today, I’m going to show you How do I get each character of string in react js, as above mentioned, I’m going to use the for...of loop method to get every char.

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

React js get each character of string example

Here, we will take string state with some data and use for...of loop method to get each char of string.

import React, { useState, useEffect } from "react";
export default function App() {
  const [str, setStr] = useState("Infinitbility");

  useEffect(() => {
    for (let char of str) {
      console.log(char);
    }
  }, []);

  return (
    <div className="App">
      {str.split("").map((char) => {
        return <p>{char}</p>;
      })}
    </div>
  );
}

To get each character of the string to render components, I have used the split("") method to convert string to array and apply map() to iterate.

I hope it helps you, All the best đź‘Ť.

Output