Arrays are similar to strings, but are a more general collection type. Like strings, arrays are a sequence of values that can be accessed via an ordered index. Unlike strings, arrays can store data of any type.
The figure below demonstrates an array of named languages. The array contains four strings, each of those values has an index position.
Programmers use multiple ways to declare a new array. The simplest way is to
use array literal notation
. Anything enclosed in the square brackets
will be items in the array. Each item should be followed by a comma
there are no items inside the brackets, then the array is considered empty.
1 2 3
Array items can also be declared on multiple lines.
1 2 3 4 5 6
To check the length of an array, use the
length property, just like with
In other languages, such as Java and C#, arrays are of a static length requiring the length of the array to be declared upon creation.
Print out the length of two arrays.
1 2 3 4 5
let grabBag = ["A string value", true, 99, 105.5];
It’s rare that you would store data of multiple types in the same array, because grouped data is usually the same type. In other languages, such as Java and C#, all items in an array have to be of the same type.
What is the length of the two arrays?
Hint: look closely at the quotes in the classes array.
1 2 3
let classes = ["science, computer, art"]; let teachers = ["Jones", "Willoughby", "Rhodes"];
How can you change the
classes array declaration to have the same number of items as the