👉What is an Array?

In JavaScript, an array is a versatile data structure used to store multiple values in a single variable.


 It allows you to group related data together and perform various operations on them.
To create an array, you can use square brackets and separate the values with commas:


let myArray = [value1, value2, value3];


Arrays can store any type of data, including numbers, strings, objects, and even other arrays. The elements in an array are accessed using their index, which starts from 0 for the first element:


let firstElement = myArray[0]; // Accessing the first element

 

Arrays have several built-in methods that enable powerful data manipulation. Some commonly used methods include:

push: Adds elements to the end of an array.

pop: Removes the last element from an array.

length: Returns the number of elements in an array.

indexOf: Returns the index of a specified element in an array.

Here's an example showcasing these methods: 

let fruits = ["apple", "banana", "orange"];
       
        fruits.push("mango"); // Adds "mango" to the end of the array
        console.log(fruits);
        fruits.pop("orange"); // Removes the last element ("orange") from the array
        console.log(fruits);
        console.log(fruits.length); // Outputs 3
        console.log(fruits.indexOf("banana")); // Outputs 1

Arrays are incredibly useful in JavaScript for tasks such as storing lists of data, iterating over elements, sorting, filtering, and much more. They provide a flexible and efficient way to work with collections of values in your JavaScript programs.

👉GO ON COMPLETE PAGE

Comments

Popular posts from this blog

Knapasack

Data types & Operators

If else condition

COMPLETE WEB DEVELOPMENT BOOTCAMP:practice.html