All ways to create arrays in js

There are several ways to create arrays in JavaScript:

  1. Arrays are created using Array Literals:
javascriptCopy code

let arr = [1, 2, 3];

  1. Arrays are created using the Array constructor:
  2. javascriptCopy code

let arr = new Array(1, 2, 3);

  1. Use the Array constructor to create an empty array of the specified length:
javascriptCopy code

let arr = new Array(3);

  1. Use the Array.from() method to create an array from an array-like object or iterable:
javascriptCopy code

let arr = Array.from('hello');

  1. Use the Array.of() method to create an array with any number of arguments:
javascriptCopy code

let arr = Array.of(1, 2, 3);

  1. Arrays are created using the Spread Operator:
  2. javascriptCopy code

let arr1 = [1, 2, 3]; let arr2 = [...arr1];

These are all the ways JavaScript creates arrays.

Guess you like

Origin blog.csdn.net/m0_62843289/article/details/130713026