Javascript
JS Arrays
Javascript Array Reference

JavaScript Array Reference

Last Updated: 16 Jan, 2024

In JavaScript, an array is like a container that holds multiple values in a single variable. Instead of creating separate variables for each item, you can store them all in an array.

Syntax

const arr = ["Item1", "Item2", "Item3", ...];

Example: Creating an array and copying its items to another array, and displaying the copied items.

// Create and initialize an array 
let courses = ["HTML", "CSS", "JavaScript", "React"]; 
  
// Display the array items 
console.log(courses); 
   
// Create a new empty array 
let newArray = []; 
   
// forEach loop to push elements 
// into new array 
courses.forEach(function (course) { 
    newArray.push(course); 
}); 
   
// Display the new array of items 
console.log(newArray); 

Output:

[ 'HTML', 'CSS', 'JavaScript', 'React' ]
[ 'HTML', 'CSS', 'JavaScript', 'React' ]

JavaScript Array Constructor

In JavaScript, a constructor gets called when an object is created using the new keyword.