Coding diary day 5: Arrays var fruits = [βπβ, βπβ, βπβ, βπβ]
Iβm back! Skipped 2 days, but trying really hard to develop a consistent study routine. Not going to beat myself up over it. Slow progress is better than no progress, so here is what I learned this morning:
Array Object
Arrays and Objects are collections of data.
- The Array object is used to store ordered list of multiple values in a single variable such as the above (see screenshot)
fruits
variable - Array indexes, just like strings, are zero-based: the first element is positioned at index 0, the second at index 1 and so on. E.g., π in the above
fruits
variable is positioned at index 0 - Arrays are considered objects in JavaScript but more on that later
Empty Arrays
- Guess what, an array does not need to have anything in it to be still considered an array
New Functions I learned today
Use variables as an argument
- Declare a function that takes an array parameter and returns it
A function that takes in an Array + Index
- Now, declare a function that takes in an array AND an index, and returns the element located at the inputted index
Create a variable inside of a Function
Up until now, Iβve only used parameters when defining functions. Now, itβs time to change it up a little. Here is a case where I create a new variable inside a function definition.
- Declare a function that takes no parameters, creates an array, and returns the newly created array
Next lesson, Objects, so stay tuned
XOXO
var emptyArray = [];