Create Array with null values in JavaScript & ReactJS – Code Example

Total
0
Shares

To create an array of size n and having null values, you can use Array(n).fill(null) function. It works in both Javascript and ReactJs. Check out this code example –

arrayOf9Values = Array(9).fill(null);

Example in React JS –

class Board extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      xIsNext: true,
    };
  }

Source: ReactJS Docs