Hi Friends 👋,
Welcome To Infinitbility! ❤️
To get the length of the array in react js, use the array length property it will return your array elements count.
The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
In the following example, we will take the sample array and use array length to get a count of the array elements.
1let arr = ["infinitbility", "aguidehub", "sortoutcode"];23console.log(arr.length) // 3
Today, I’m going to show you How do i find the length of a array in react js, as mentioned above here, I’m going to use the use array length property to get length of json array.
Let’s start today’s tutorial how do you get the length of the array in react js?
In this example, we will do
- take an example of array state
- use the array length
- print the length of array on the page screen
1import React, { useState, useEffect } from "react";2import "./styles.css";3export default function App() {4 const [sampleArray, setSampleArray] = useState(["infinitbility", "aguidehub", "sortoutcode"]);56 return (7 <div className="App">8 <h1>{`sampleArray length is ${sampleArray.length}`}</h1>9 </div>10 );11}
In the above react js example, we have taken the sample array state, find the array length, and print it on the screen.
I hope it helps you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.