[Code Example] Create Array with null values in JavaScript & ReactJS

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

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like