Javascript Arrays

Javascript Arrays

This course provides an overview of Javascript Arrays. This course will teach You how arrays function and how to use array methods. Introduction to Arrays

Table Of Content

  1. What is an array?

  2. How to Create an Array with JavaScript

  3. What are Array Methods?

4 . What are the many types of Javascript methods?

  1. How to use a Javascript array.

  2. Finally,

Prerequisite:

To comprehend this course, you must have a basic understanding of HTML and CSS.

ARRAYS

Arrays are list-like objects that are used to store several things in a single variable. When We use an array, we don't use curly braces; instead, We use "[]" and strings to list all the elements. Arrays, in a nutshell, are containers that can hold more than one item. Consider this: Jude has a closet in which he may keep his shoes, clothes, and perfumes; We can also link this to arrays.

WHAT IS AN ARRAY?

Remember how We said in the previous lesson that We write arrays in square brackets? When utilizing Arrays, the numbering normally begins with 0.

Const name = ["Becky", Noah, Alwell, Phillip"]; console.log(name[0]);

METHODS OF ARRAYS

Array methods are JavaScript methods that are used to do various tasks, such as adding a new item to the beginning or end of an array and deleting items from the beginning or end of the array. Methods such as push(), unshift(), pop(), and shift() are available. We will investigate each of these strategies.

Push()

Push is one of the methods used in arrays. Its primary function in an array is to add items to the array's end. Let's look at an example to get a better understanding of this method. Const trip=["Lagos", "Abuja", "Benin"]; trip.push("Togo"); console.log(state); This appends "Togo" to the end of the arrays. It is currently in Lagos, Abuja, Benin, and Togo. Please keep in mind that You can add several products.

unshift()

Unshift() is another array function that can be used to add items to a variable; however, unlike the push method, it is used to add items to the beginning of an array. Consider the following: Const name["Blessing”, "Ajiri", "Yoma"] console.log(name); name.shift("Miracle");

Pop()

Pop is an array method for removing an item from the end of an array. Const food = ["rice", "beans", "yam"] food.pop([2]);

Remember that We said at the beginning that We can access our arrays using index numbers; thus, this will automatically delete "yam from the end of the array. It is now "rice" and "beans."

Shift() This removes the first item from the Array, it is the opposite of pop(). For example Const car =[“Telsa”, “Camry”,” Venza”] car.shift(“Tesla);

In the pop() method, We used index numbering to remove the item but here, this is another method you can use when trying to delete or add an item, You can either use the item you are trying to remove and add or you can use the index numbering.

How To Access an Array

An array can be accessed using index numbering in JavaScript. In JavaScript, When counting an item, the numbering usually starts from 0. Also, take note, We can use( for..of)

Example Const Car=[“benz”, “Venza”, “Toyota”];

Now take note as we access this variable

Console.log(Car[0]);

When We check the output, it gives us “Benz”.

Now when using for...of, we use it this way:

Const age = [12, 20, 15, 20]; For ( const year of age){ console.log(age); }

Exercises:

Although this is not required, it will help you enhance your knowledge of arrays.

  1. Provide an array of items and use push to add more.

  2. Using no. 1, use what we studied in the course to access one of the mentioned Arrays.

CONCLUSION I hope you learned a lot from this lesson, We addressed arrays, how to utilize arrays, array methods, the many types of methods that we can use in arrays, and how to access arrays in our Javascript.