Variables

Arrays in JavaScript

Used to declare a variable that can hold multiple values.


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

Notes

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.


Example
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
Documentation

Array - JavaScript | MDN

Add to Slack

< Global Variables   |   Determine Type >

© 2019 SyntaxDB. All Rights Reserved.