Assuming we want to keep the record of available cookies in a grocery store, where we have multiple values of available cookies, we have lots of lollipops, ice-creams, vanillas and more all available in the store.
Assigning each available values to a variable is fine if we only have few available cookies in the store.
Let cookie1 = “strawberry-cookies”
Let cookie2 = “Stick-sweets”
All variables store each values of all the available cookies separately in the memory of the system, and makes our program more complicated and unclean.
What if we have more available cookies ? Then, we need an array, we need a collection of all the available cookies. An Array is basically the collection of values as explained.
Cookies = [“strawberry-cookies”, “Stick-sweets”, “Lollipop”, “Kolas“, “Pinky” ]
all the available cookies are all stored in a single variable name, they are stored and can access with their index.
Array are collection of values, An array is also a special variable, which can hold more than one value at a time.
let cookies = new Array(“strawberry-cookies”, “Stick-sweets”, “Lollipop”, “Kolas“, “Pinky”)
or we assign a square bracket to a variable, both way works fine but like this below is more efficient and clean
let cookies = [“strawberry-cookies”, “Stick-sweets”, “Lollipop”, “Kolas“, “Pinky” ]
As said earlier, everything in an array can be changed or manipulated thru its index, for example if we want to change the color pinky to ice-creams ?
we will have to access the value thru its index which is the 4th index of the cookies array
cookies = ‘ice-creams’
Arrays can be used for lots of purposes in our program.