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”) 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’