There are several ways to create arrays in JavaScript:
- Arrays are created using Array Literals:
javascriptCopy code
let arr = [1, 2, 3];
- Arrays are created using the Array constructor:
- javascriptCopy code
let arr = new Array(1, 2, 3);
- Use the Array constructor to create an empty array of the specified length:
javascriptCopy code
let arr = new Array(3);
- Use the Array.from() method to create an array from an array-like object or iterable:
javascriptCopy code
let arr = Array.from('hello');
- Use the Array.of() method to create an array with any number of arguments:
javascriptCopy code
let arr = Array.of(1, 2, 3);
- Arrays are created using the Spread Operator:
- javascriptCopy code
let arr1 = [1, 2, 3]; let arr2 = [...arr1];
These are all the ways JavaScript creates arrays.