Learn array and it's method in javascript

Learn array in easy way.

Hi Everyone,

.

.

let's start

What is array?

array is basically collection of data that stores the multiple values between square[' '] bracket. the square bracket represent the it is an array.

to create array as follows :

let myArray = [];

the above shows the syntax that create an empty array.

we can access an individual element in an array by its index, which starts at 0.

let fruits = ['apple', 'grapes', 'orange'];
console.log(fruits[0]); // 'apple'
console.log(fruits[2]); // 'orange'

There are the following methods in array :

  • push() :

The push() method adds one or more elements to the end of an array and returns the new length of the array. let's see the example:

let myArray = ["apple", "grapes", "cherry"];
myArray.push("date");
console.log(myArray); // ["apple", "grapes", "cherry", "date"]

You can also use the push() method to add elements to an empty array:

const emptyArray = [];

emptyArray.push('user', 'name');

console.log(emptyArray);
// Output: ['user', 'name']

In this example, the push() method adds two new elements ('user' and 'name') to the empty emptyArray.

  • pop() :

The pop() method removes the last element from an array and returns that element. let's see the example:

let myArray = ["apple", "grapes", "cherry", "date"];
let lastElement = myArray.pop();
console.log(lastElement); // "date"
console.log(myArray); // ["apple", "grapes", "cherry"]
  • shift() :

The shift() method removes the first element from an array and returns that element. let's see the example:

let myArray = ["apple", "grapes", "cherry", "date"];
let firstElement = myArray.shift();
console.log(firstElement); // "apple"
console.log(myArray); // ["grapes", "cherry", "date"]
  • unshift() :

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. let's see the example:

let myArray = ["apple", "grapes", "cherry"];
myArray.unshift("date");
console.log(myArray); // ["date", "apple", "grapes", "cherry"]
  • slice() :

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. let's see the example:

let myArray = ["apple", "grapes", "cherry", "date", "elderberry"];
let newArray = myArray.slice(2, 4);
console.log(newArray); // ["cherry", "date"]
  • splice() :

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements. let's see the example:

let myArray = ["apple", "grapes", "cherry", "date", "elderberry"];
myArray.splice(2, 2, "orange", "pear");
console.log(myArray); // ["apple", "grapes", "orange", "pear", "elderberry"]

In the example above, we remove 2 elements starting from the index 2, and replace them with the strings "orange" and "pear".

  • concat() :

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. let's see the example:

let fruits = ["apple", "grapes", "cherry"];
let vegetables = ["carrot", "eggplant", "garlic"];
let food = fruits.concat(vegetables);
console.log(food); // ["apple", "grapes", "cherry", "carrot", "eggplant", "garlic"]
  • map() :

The map() method creates a new array with the results of calling a provided function on every element in the calling array. let's see the example:

const users = [
  { name: 'Alice', age: 28 },
  { name: 'Bob', age: 35 },
  { name: 'Charlie', age: 42 }
];

const userNames = users.map(function(user) {
  return user.name;
});

console.log(userNames);
// Output: ['Alice', 'Bob', 'Charlie']

In the above example, the map() method is used to create a new array of user names. The function passed to the map() method takes one parameter user, which represents the current element being processed in the array. The function returns the name property of the current user object, which is then added to a new array named userNames.

The map() method is a powerful tool for transforming arrays in JavaScript. It's often used to create new arrays based on existing ones, or to extract specific information from an array of objects.

You can also use the length property of an array to get the number of elements it contains:

let myArray = [1, 2, 3, 4, 5];
console.log(myArray.length); // 5

Overall, the syntax for working with arrays in JavaScript is easy to learn and use. With a few simple methods, you can manipulate and access the elements of an array to create complex programs and applications.

Did you find this article valuable?

Support Danish Rayeen by becoming a sponsor. Any amount is appreciated!