Arrays in JavaScript

Used to declare a variable that can hold multiple values.

var arrayName = [value1, value2, value3]; //initialize an array, values optional
arrayName.push(value4); //adding an element 
arrayName[index]; //accessing an element in the array


Calling the .length method on an array will return the length of the array.
Accessing an array index out of bounds will return 'undefined'.
The first element in the array is at index 0.

Arrays can hold values, objects, or even functions.

var grades = ['85', '75', '100'];
grades.push('60'); //will add 60 to the array
grades[1]; //will return 75
grades[10]; //will return undefined

See Also

Array - JavaScript | MDN

Add to Slack

< Global Variables   |   Determine Type >

© 2019 SyntaxDB. All Rights Reserved.