Solution
To get length of string in react js, use string.length
property it will count and return number of char of string.
string.length
The length
property of an String
object represents the number of letter in that string
.
The value of the length property is a non-negative integer with a value less than 232.
Snippet
In this snippet, we will create an string and use the string.length
property to show the length of string.
const str = "ReactJsSnippet";
let length = str.length;
console.log(length); // 14
Example
In this example, we will show the length of string in the console and page.
Let’s start coding…
import React, { useEffect, useState } from "react";
export default function App() {
const [str, setStr] = useState("ReactJsSnippet");
useEffect(() => {
console.log("str length", str.length)
}, []);
return (
<div className="App">
<h1>{`String Length`}</h1>
<p>{str.length}</p>
</div>
);
}
Output