Array Basics
Array data structures are going to share similar functionality between all languages.
The syntax for a js array is going to look familiar to that of a traditional Java array, the major exception being that they are not typed.
It functions, however, in a manner more similarly to that of an ArrayList.
JS arrays actually have a number of functions tied to their class that allow them to be an extremely versatile data structure.
Creating Arrays¶
To create a new array literal with no starting values, we simply use []
let nums = [];
If we instead wanted to create an array with values, we could list the values separated by a comma.
let nums = [1,2,3,4];
let names = ["Andrew", "Kris"];
Accessing Properties and Values¶
Once an array has been created we can check how many elements are stored inside of it with the .length property.
let nums = [1,2,3,4];
nums.length; // 4
To access a specific element in an array we can use subscript notation, just like in Java.
let nums = [1,2,3,4];
nums[2]; // 3
Adding to an Array push(value)¶
Add an item to the end of an Array and return the resulting length of the Array.
let arr = [1,2,3];
arr.push('kiwi'); // 4
arr; // [1,2,3,'kiwi']
Skill Drill¶
- Create a folder ArrayHandsOn in
SD/JS. Inside of this location create anarray.jsfile and anarray.htmlfile. We will use this file for all of the hands ons in this chapter.Assign an empty array to a variable named
classmates.Use
push()to add the names of your classmates to the array.
Removing from an Array pop()¶
Remove and return the last item in an Array.
let arr = [1,2,3,4];
arr.pop(); // 4
arr; // [1,2,3]
Skill Drill¶
Use
pop()to store the last name from yourclassmatesarray into a variable namedlast.Use
console.logto print outlast, and use anotherconsole.logto print out theclassmatesarray.