November 29, 2015 • ☕️ 2 min read
. Elements in an array are separated by a comma. An array can have any type of data, in any order, inside of the brackets.
var myArray = ["John", 39, "Tiffany", 37, false];
is a typical array. Usually we can get the index, or position, of a piece of data in an array by bracket or dot notation.
You can initialize an array as follows:
var myArray = ;
You can also initalize an empty array such as:
var emptyArray = ;
This is useful for having a place to hold values that will be initialized later in your code.
var indexOfArray = ["Jerri", 69, "Tiffany" 37, false]; console.log(indexOfArray); // This would return the boolean value 'false' console.log(indexOfArray.); // This is accessing an array with dot notation. // This would return 'Tiffany' console.log(indexOfArray.1); // This isn't proper syntax as properties that begin with // a number can't be accessed using dot notation console.log(indexOfArray.0); // This is okay as it is a string
You can see how many items are in an array with the
.length property. The syntax is similar to accessing an index with dot notation:
var howMany = ["Jerri", 69, "Tiffany" 37, false]; console.log(howMany.length); // this will return 5 as there are 5 items in the array. // Not to be confused with *indeces*