JAVASCRIPT

Creating and Populating Arrays


 

Creating an Array:

To create an array, we can use the array literal syntax, which involves enclosing the elements inside square brackets [].
 

// Declaring an empty array
let myArray = [];

 

Filling an Array:

The fill() method in JavaScript is used to fill (or populate) elements of an array with a specified value over a defined range. It allows you to manipulate the existing array by replacing the elements within the specified range with the given value.

 

Syntax:

// Syntax
arr.fill(value, start, end);

 

Example:

// example
let myArray = [1, 2, 3, 4, 5];
myArray.fill(0, 1, 3);
console.log(myArray); // Output: [1, 0, 0, 4, 5]