;

Learn Basics Array In Javascript

What is Array in Javascript ?


How to learn Array in Javascript

buayaberdiri.blogspot.com - Arrays in JavaScript are a data structure that allows you to store and manipulate a group of values. They are similar to lists in other programming languages, and are an essential tool for any JavaScript developer to understand. In this article, we will cover the basics of arrays in JavaScript, including how to create and modify them, how to access and loop through their elements, and how to use some of the built-in array methods.

How to Creating Arrays


To create an array in JavaScript, you simply use the array literal notation, which is a pair of square brackets [] with values separated by commas. For example:

let myArray = [1, 2, 3, 4, 5];

This creates an array with five elements, each containing a number from 1 to 5. You can also create an array with a mix of different data types, like this:

let mixedArray = [1, "hello", true, [1, 2, 3]];

This creates an array with four elements, the first being a number, the second a string, the third a boolean, and the fourth another array.

You can also create an empty array by simply using the brackets with no values:

let emptyArray = [];

Modifying Arrays


Once you have created an array, you can modify it in various ways. You can add new elements to the end of the array using the push() method:

myArray.push(6);

This will add the number 6 to the end of the array, making it [1, 2, 3, 4, 5, 6]. You can also add new elements to the beginning of the array using the unshift() method:


myArray.unshift(0);


This will add the number 0 to the beginning of the array, making it [0, 1, 2, 3, 4, 5, 6].

You can also remove elements from the array using the pop() method, which removes the last element of the array:


myArray.pop();


This will remove the number 6 from the end of the array, making it [0, 1, 2, 3, 4, 5]. You can also remove the first element of the array using the shift() method:


myArray.shift();


This will remove the number 0 from the beginning of the array, making it [1, 2, 3, 4, 5].

You can also modify specific elements of the array by using their index. For example, to change the second element of the array to "world", you would do this:

myArray[1] = "world";


This would change the array to [1, "world", 3, 4, 5].

Accessing Array Elements


To access the elements of an array, you can use the index of the element, which is the position of the element in the array. For example, to access the second element of the array, you would do this:

var secondElement = myArray[1];

This would assign the value "world" to the variable secondElement.

You can also use a loop to access all the elements of an array. One way to do this is with a for loop, like this:

for (var i = 0; i < myArray.length; i++) {
    console.log(myArray[i]);
    }    

This would log all the elements of the array to the console. Another way to loop through an array is with the forEach() method, which allows you to pass a callback function that will be called for each element of



Check out other articles about Javascript 

  1. Learn Basics Asynchronous In Javascript





List of Article Posts https://buayaberdiri.blogspot.com