Solution
To get length of array in react js, use array.length
property it will count and return number of element of array.
array.length
The length
property of an Array
object represents the number of elements in that array
.
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 array and use the array.length
property to show the length of array.
const arr = ["aguidehub", "infinitbility", "sortoutcode"];
let length = arr.length;
console.log(length); // 3
Example
In this example, we will show the length of array in the console and page.
Let’s start coding…
import React, { useEffect, useState } from "react";
export default function App() {
const [arr, setArr] = useState(["aguidehub", "infinitbility", "sortoutcode"]);
useEffect(() => {
console.log("arr length", arr.length)
}, []);
return (
<div className="App">
<h1>{`Array Length`}</h1>
<p>{arr.length}</p>
</div>
);
}
Output