How to use arrays in javascript

How to use arrays in javascript

Arrays are a great way of initializing several things into a variable. For example

  const arrOfNum = [ 1, 2, 3, 4 ]
  const participants = [ Daniel, James, Fried, Jakob ]

The participants which is a better example show all participants and initialize it( attributes it ) to a variable.

There are many types of Initialization from Arrays to Objects to Boolean to String and more and each one is written differently and has different uses.

Since the topic is about arrays, we shouldn't dive too far from it. Read more on Arrays To be able to call or use one of the many details in an array, we have to call it with a special syntax. Calling "Daniel" alone from the participant will be calling the array itself (participants) and adding an index number of the variable you want to reach.

It is like calling a boy out of 10 boys in a row(This is just an example, I'm not trying to be sexist in any way), To call the first boy is to call the number 1 boy and the last boy will be number 10 and if the number is unknown, the last boy number will be the total number of the boys since the first is 1, the second is 2, the previous will mathematically be Total numbers of the boys

That is precisely how it works for computers as well but the computer's first number isn't number 1, it is 0 and so to call "Daniel" the computer will call the number 0 participant but in an everyday human situation, humans will call the number 1 of participants

    const participants = [ Daniel, James, Jakob ]

    // To call Daniel
    console.log( participants[0] )

so to call James would be

    const participants = [ Daniel, James, Jakob ]

    // To call James
    console.log( participants[1] )

and to call Jacob which is the last would be

    const participants = [ Daniel, James, Jakob ]

    // To call Jakob
    console.log( participants[2] )

    // Jakob is the last variable but what if the variables were large to count

    // We use participants.length to find the total length of the participant which is calculated in human form(It doesn't start counting from 0) and - 1 to give the last number

    console.log( participants[participants.length - 1] )

I'm logging this detail out so to see them, you have to open your live server, inspect and check the console to see how these codes work In case you don't know how to use the console, here is a quick video to help you through There are many things that can be done to an array, like increasing its variables

    participants[20] = "Manny"

    // or you can push it to the array 
    participants.push("Jay")

    console.log(participants)

You can concatenate two arrays or more into one so [1, 2] and [3, 4] could come to form [1, 2, 3, 4] or [3, 4, 1, 2] depending on how it was concatenated

  const arr1 = ["Cecilie", "Lone"];
  const arr2 = ["Emil", "Tobias", "Linus"];
  const children = arr1.concat(arr2);
  console.log(children)

You could also filter an array to only include whatever you want while disposing of the rest, like when you search for an item online, the arrays of the items are filtered so only your letters match will show because they are what is left in the array

const ages = [32, 33, 16, 40];
const result = ages.filter(checkAdult);

function checkAdult(age) {
  return age >= 18;
}

The crazy thing is that an array can contain objects, string, numbers, boolean, even another array or more arrays

const arr = [ 1, 2, [0, 1, 2], 90, 9, true, "Making baskets", {
                      name: "code_art4"
                    }]

console.log(arr[2]) // The answer would be [0, 1, 2]
console.log(arr[2][1]) // The answer would be 1

console.log(arr[7]) // The answer would be {name: "code_art4"} which is an object 

console.log(arr[7].name) //The answer would be code_art4

To delete things from an array can be used with slice

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);

This is getting quite long for brief teaching but I hope this was impactful in you need to know more on arrays, you can check w3schools or mozilla docs on Javascript Arrays

Don't be discouraged by how hard it is, we all had to do them and it gets better when you understand the syntax