There are nine units in Lambda School’s Full Stack Web Development Pre-course. I’m going to summarize what the pre-course covers for each of them. This material is basically ground-floor information that students need to know before they start the full course.

Introduction to Arrays
An array is a special JavaScript object that holds a collection of data. Just like a variable holds a single piece of data (a string, a number or a boolean value) an array holds multiple pieces of data. Items in an array don’t have to be the same type, so they can contain strings and boolean values, for example. Strings are enclosed in quotation marks, and each item is separated by a comma.
An array is declared just like a variable, but after the equals sign, it has brackets ([]). Optionally, data can be immediately stored in the array, during its creation. A common misconception is that an array is a collection of variables – which it is in other programming languages. In JavaScript, its a single variable with a collection of data, or elements.
As per the Lambda School text:
In the first Javascript lesson we discussed the 3 basic data types (strings, numbers, and booleans), and how to assign those data types to variables. We discussed how a variable can only point to a single string, number, or boolean. In many cases though we want to be able to point to a collection of data types. For example what if we wanted to keep track of every student’s name in this class using a single variable, studentsNames. We can do that using Arrays. We can think of arrays as storage containers for collections of data. Building an array is simple, declare a variable and set it to []. We can then add however many strings, numbers, or booleans as we want to the container (comma separated), and access those items whenever we want.
const studentsNames = ['Dan', 'Maria', 'Sara', 'Raj']; let bands = ['Godflesh', 'Pitch Shifter', 'Ministry', 'Cold World']
Accessing Items in an Array
Elements are indexed (or numbered/counted) starting at 0, so the first item in an array is item 0, the second is 1, the third is 2, and so on. Due to this, the length of an array is the number of actual elements in it, minus 1. Elements are called by their index number, preceded by the name of the array.
As per the Lambda School text:
We can access an item at anytime in an array, we just need to call the item by its position in the array. Items are given a numerical position (index) according to where it is in the array, in order. An array’s numerical order ALWAYS starts at 0, so the first item is in the 0 index, the second in the 1 index, the third in the 2, and so on (this can be tricky at first, but just remember arrays always start at 0).
const studentsNames = ['Dan', 'Maria', 'Sara', 'Raj'];
0 1 2 3
To access an item in an array, type the name of the array variable, followed by brackets containing the numerical assignment/index:
const studentsNames = [‘Dan’, ‘Maria’, ‘Sara’, ‘Raj’]; console.log(studentNames[1]); // 'Maria'
let bands = ['Godflesh', 'Pitch Shifter', 'Ministry', 'Cold World'] console.log(bands[3]); // 'Cold World'
Like functions, arrays have their own built-in methods. The following pages go over some of them:
- w3schools: JavaScript Array Methods
- JavaScript.info: Array Methods
- dev.to: 10 JavaScript array methods you should know
As per the Lambda School text:
To dynamically access the last item in the array, we will use the .length method. In our studentsNames array, the length is 4. We know the first item is always going to be 0, and every item after is shifted over one number. So in our example the last item has an index of 3. Using our length property we will show how it is done when we don’t know the number of items in an array:
const studentsNames = [‘Dan’, ‘Maria’, ‘Sara’, … ,’Raj’]; console.log(studentNames[studentNames.length - 1]); // 'Raj'
Assignment
The value of elements in an array can be changed. As per the Lambda School text: We can assign and reassign any index in the array using the bracket/index and an =.
const studentsNames = [‘Dan’, ‘Maria’, ‘Sara’, ‘Raj’]; studentNames[0] = 'Ryan'; console.log(studentNames); // ['Ryan', 'Maria', 'Sara', 'Raj'];
2 thoughts on “Lambda School Pre-course 16: Learn to use arrays to store and access data”