From the course: Objects in JavaScript: A Dynamic Data Structure
Arrays - JavaScript Tutorial
From the course: Objects in JavaScript: A Dynamic Data Structure
Arrays
So, let's dive in. You are already familiar with what data structures are in general and why we use them in computer science. If you have any questions or are unclear, I would suggest revisiting the What You Should Know movie on data structures, and if necessary, take a quick refresher course specifically on data structures. For this part of the course, you will focus on reviewing what arrays are and how to create arrays. In JavaScript, an array can be instantiated as a single variable that is used to store elements of differing data types. Initiate is a fancy technical term for creating. When instantiating an array, you can declare a new variable and assign its value. You can also use the new operator. A great reference for the new operator is MDN's documentation, which adheres to ES2016 standards. Readability is important. So, in this course, we will simply use the array literal method. Okay. So, let's walk step by step through how to declare an array, assign a value, and reassign the value as a refresher. First, you will declare a variable that you can label anything you want. For this example, let's say you label it arrOne. Second, you will assign the variable of value, and here, you can assign it the value of an array with three numbers. The value in this array will be four, five, and six. Third, to reassign the data to the array, you will simply type the variable and assign it a new value. Here we see arrOne has been reassigned the values of four, five, six, and seven in an array. Lastly, it's important that after storing your data that you be able to access it. As accessing data is extremely important for engineers in creating applications. Many companies no longer keep paper filing cabinets, and thus, most company data is virtual. To see the contents of the array, remember, you can simply use console.log to print, and this will show you the elements of the array. Here on line five, we see console.log arrOne, and it shows us our array with the values of four, five, six, and seven after reassignment. Also, you are able to access each element of the array using its indices. For example, here you can see that our array with the label of arrOne, and we have a bracket with a zero inside of it here on line three. Well, remember, you reassigned the value of the array. And so, now, the array has four values. So, arrOne at index zero has the value of four as the first volue. You covered a lot in this part of this series as we went over arrays. If you need to go back, feel free to rewind to cover the material again. Otherwise, next up, you will cover the fundamental concepts about objects before we compare and contrast arrays and objects.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.