A list that keeps several pieces of data in order.
Arrays are helpful for storing related data. They can contain data of any type. For example, an array could contain a list of someone's favorite colors as strings. Arrays are created as follows:
let myArray = ['deeppink', 'darkorchid', 'magenta'];
Each piece of data in an array is called an element. Each element has an address, or index, within its array. The variable myArray
refers to an array with three String elements, 'deeppink'
, 'darkorchid'
, and 'magenta'
. Arrays are zero-indexed, which means that 'deeppink'
is at index 0, 'darkorchid'
is at index 1, and 'magenta'
is at index 2. Array elements can be accessed using their indices as follows:
let zeroth = myArray[0]; // 'deeppink'
let first = myArray[1]; // 'darkorchid'
let second = myArray[2]; // 'magenta'
Elements can be added to the end of an array by calling the push() method as follows:
myArray.push('lavender');
let third = myArray[3]; // 'lavender'
See MDN for more information about arrays.
Ejemplos
Referencias Relacionadas
Array
Una lista que mantiene diferentes tipos de datos en orden Los arreglos son útiles para almacenar datos relacionados.
class
Una plantilla para crear objetos de un tipo en particular.
for
Una forma de repetir un bloque de código cuando se conoce el número de iteraciones.
function
Un grupo de declaraciones con nombre.