DEV Community

Shoyab khan
Shoyab khan

Posted on

Day 9 of 30 of JavaScript

Introduction

Hello, readers! 😊 I hope you’re doing well. In our previous post, we discussed functions and their applications in JavaScript. Today, we’ll continue our journey by exploring arrays and the various methods used to manipulate them. Let’s get started!

What are Arrays?

An array is a data structure that holds a list of similar types of elements. For instance, you can store a collection of ten numbers in an array.

Declaration of Arrays

There are several common methods to declare an array:

  1. Using Array Literals: You can simply declare an array variable and assign a list of values to it.
   const numbers = [1, 2, 3, 4, 5];
Enter fullscreen mode Exit fullscreen mode
  1. Using the Array Constructor: You can create an array using the Array constructor.
   const numbers = new Array(1, 2, 3, 4, 5);
Enter fullscreen mode Exit fullscreen mode
  1. Creating an Empty Array: You can declare an empty array and then assign values to each index separately.
   const numbers = [];
   numbers[0] = 1;
   numbers[1] = 2;
Enter fullscreen mode Exit fullscreen mode
  1. Using the Constructor with Size: You can also create an array by specifying its size and then filling it using a loop.
   const numbers = new Array(5);
   for (let i = 0; i < numbers.length; i++) {
       numbers[i] = i + 1; // Assigning values
   }
Enter fullscreen mode Exit fullscreen mode

Note: Array indices start at 0 and go up to the size of the array minus one (e.g., arr[0], arr[1], etc.).

Accessing Array Elements

You can access elements in an array by referring to their index number. For example:

const fruits = ['Mango', 'Orange', 'Kiwi'];
console.log(fruits[0]); // Outputs: Mango
Enter fullscreen mode Exit fullscreen mode

Note: While we often say arrays hold similar types of entities, JavaScript arrays can actually store elements of different types. This flexibility allows for mixed data types within the same array.

Adding Elements to an Array

The simplest way to add a new element to an array is by using the push() method, which appends the element to the end of the array:

fruits.push('Banana'); // Adds 'Banana' to the end
Enter fullscreen mode Exit fullscreen mode

If you want to insert an element at a specific position, you can use the splice() method:

fruits.splice(1, 0, 'Pineapple'); // Adds 'Pineapple' at index 1
Enter fullscreen mode Exit fullscreen mode

In this example, the first parameter is the index where the new element should be added, the second parameter is the number of elements to remove (0 in this case), and the third parameter is the new element to be added.

You can also use the spread operator (...) to add elements to an array:

const newArr = [0, ...fruits, 4]; // Combines elements into a new array
Enter fullscreen mode Exit fullscreen mode

Length of an Array

The length property of an array returns the number of elements it contains:

console.log(fruits.length); // Outputs the number of elements in the fruits array
Enter fullscreen mode Exit fullscreen mode

Conclusion

That’s all for today’s post! I hope you found this information helpful. We’ll continue our exploration of arrays in the next blog. Until then, stay connected, and don’t forget to follow for more updates! 😊

Top comments (0)