React State Array Get Length Example

  • Author: Techiediaries Team

In this quick tip article, we'll see how to get the length of an array or state array in React.

React is a JavaScript library for rendering user interfaces therefore it simply deals with the UI and doesn't provide its own utilities for handling arrays or similar tasks. As a result, you simply need to use the built-in JavaScript methods and APIs.

JavaScript' Array Length by Example in React

In this example, we'll see how to get the length of an array in React and JavaScript.

JavaScript already provides many built-in methods for getting the length of an array, let's see how to use in a React example.

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. Source

Create an index.js file and add the following code:

import React from 'react'

class App extends React.Component {

        const array = ["React","is", "awesome", "!"];
        const length = array.length;
                <p>Array length is { length }.</p>


ReactDOM.render(<App />, document.getElementById("root"));

In the render() method, we simply define a JavaScript array then we get the length of the array using the Array.length method. Finally we return a JSX markup to didplay the length of the array.

You should import React and ReactDOM in your project and use ReactDOM to mount the component in the DOM:

<script src=""></script>
<script src=""></script>

<div id="root"></div>

In the same way, you can get the length of the array in React state as follows:

class App extends React.Component {
  state = {
    array: ["Hello", "React"]

  render() {
    return (
        <p>React State Array Length: {this.state.array.length}</p>        

ReactDOM.render(<App />, document.getElementById("root"));


In this quick example, we've seen how to use the Array.length method to get the length of a local array or state array and render the result in the DOM.

Angular Cookbook
What's new with Angular 12
An Angular 11 Roadmap - The Past, Present, and Future of Angular
Angular 11 Developer Roadmap
Vue 3 Playground