KODE WITH KLOSSY
Welcome to Introduction to Arrays!
KEY POINT
Let's write down the learning goals and technical vocabulary for today's lesson!
🎯 LEARNING GOALS
Understand what an array is and why they are useful
Use JavaScript syntax to declare and modify an array
📗 TECHNICAL VOCABULARY
Array
Collection
Element
Index
💪 WARM-UP
In the repl below, declare a variable for 10 scholars in your cohort. The variable names should be something like scholar1, scholar2, etc. and the values should be strings of their first names.
Make sure you are editing the .js file in the file tree and looking at the console for output.
Code check! 👇
Random Elements
THINK TO YOURSELF
Where might TikTok and Instagram use arrays?






Write this down in your journal!
Access an Element
Each element in an array is automatically assigned a number called an index. This index can be used to access a specific element inside the array. Indices begin at 0 and count up. If we look back at our trending array, the following would be true.
Length
We can also check how many elements are in an array with the .length property
Updating Elements
Adding Elements
Removing Elements
👩💻 Let's get coding! Open up a repl with the name "introToArrays" and play the video below.
TRY-IT #1 👩💻👉
In the repl.it, create a variable that stores an array of at least 4 strings - you choose what the content is about. The variable name should describe the type of data those 4 strings hold.
Write a series of console.log() statements: print out the first element, the last element, and then the second element.
Please note: the following carousel has 4 videos. Please press the right arrow to go onto the next video.
Access an Element
Each element in an array is automatically assigned a number called an index. This index can be used to access a specific element inside the array. Indices begin at 0 and count up. If we look back at our trending array, the following would be true.
Length
We can also check how many elements are in an array with the .length property
Updating Elements
Adding Elements
Removing Elements
TRY-IT #2👩💻👉
For this, you will use the array you wrote in the previous Try It.
Practice accessing specific elements. Make sure to console.log() to verify you are accessing what you think you are.
Now, add two new elements into your array. Use a console.log() to make sure they have been added.
Lastly, remove at least two elements from your array. Again, make sure they have been successfully removed by printing to the console.
⚡️ RANDOM ELEMENTS
Please note: the following carousel has 2 videos. Please press the right arrow to go onto the next video.
Random Elements
THINK TO YOURSELF 🧠
Considering this section on selecting random elements from an array, answer the following in a notebook:
-
What does Math.random() do?
-
Why did we choose to multiply Math.random() by 20 for this example?
-
What does Math.floor() do?
-
Why do we have to pass an argument, or put a number inside the parenthesis for Math.floor()?
-
Is it possible for this function to ever return the same number? Why or why not?
Check your answer! 👇
Access an Element
Each element in an array is automatically assigned a number called an index. This index can be used to access a specific element inside the array. Indices begin at 0 and count up. If we look back at our trending array, the following would be true.
Length
We can also check how many elements are in an array with the .length property
Updating Elements
Adding Elements
Removing Elements
💪 PRACTICE
Declare a variable called following that stores an array of your top five favorite accounts to follow on social media, in strings.
Change the value of one element in the array
Remove the last account of the array
Add another account to the array
Print the value of the first element of the array
Print one account to the console, at random.