loading...
Cover image for Javascript Arrays for Absolute Beginner

Javascript Arrays for Absolute Beginner

seyifunmitanjs profile image Oluwaseyifunmi ・2 min read

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.
In javascript array values are stored and accessed through the index, javascript is a zero based programming language so, in an javascript array the first value is stored on the 0 index.

Arrays are a special type of objects. The typeof operator in JavaScript returns “object” for arrays.
But, Javascript arrays are still best described as arrays.

Array in javascript have lots of built-in methods that makes the concept productive enough and fun to use.

Creating an Array in Javascript

To create an array in javascript we can use the new array keyword

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[4] = ‘ice-creams’

Arrays can be used for lots of purposes in our program.

We have lots of built-in methods in javascript, let check them out and build a fun project.
Shalom

Posted on Jan 8 by:

seyifunmitanjs profile

Oluwaseyifunmi

@seyifunmitanjs

Web-dev & JAM-STACK Passionate about impacting check my blog http://howtothese.xyz #sveltejs

Discussion

markdown guide