Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get the length of the string in react js, use the String length property it will return your string char count.

The length property of a String object contains the length of the string, in UTF-16 code units. length is a read-only data property of string instances.

In the following example, we will take the sample string and use string length to get a count of the string.

let str = "infinitbility";

console.log(str.length) // 13

Today, I’m going to show you How do i find the length of a string in react js, as mentioned above here, I’m going to use the use String length property.

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

In this example, we will do

  1. take an example of string state
  2. use the string length
  3. print the length of string on the page screen
import React, { useState, useEffect } from "react";
import "./styles.css";
export default function App() {
  const [sampleString, setSampleString] = useState("infinitbility");

  return (
    <div className="App">
      <h1>{`infinitbility length is ${sampleString.length}`}</h1>
    </div>
  );
}

In the above react js example, we have taken the sample string state, find the string length, and print it on the screen.

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