Array Basics

Creating and using arrays.

Examples

Creating an Array

Using an array literal to create an array.

const cars = ["Saab", "Volvo", "BMW"];
console.log(cars);

Array with different types

Arrays can hold values of different types.

const myArray = [
  "Hello",
  42,
  true,
  { name: "John" },
  function() { alert("Hi"); }
];
console.log(myArray[0]); // Hello
console.log(myArray[3].name); // John

New Array()

Using the 'new Array()' constructor (not recommended for simple arrays).

const cars = new Array("Saab", "Volvo", "BMW");
console.log(cars);

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.