DEV Community

Cover image for Array in JavaScrip
Ezhil Arasan
Ezhil Arasan

Posted on

Array in JavaScrip

What is an array?
An array in JavaScript is a global object used to store an ordered collection of multiple values under a single variable name. JavaScript arrays are zero-indexed, dynamic in size, and can hold a mix of different data types

Why Use Arrays?
If you have a list of items (a list of bike names, for example), storing the names in single variables could look like this:

let bike1 = "Pulser";
let bike2 = "apache";
let bike3 = "duke";

Enter fullscreen mode Exit fullscreen mode

Creating an Array

Using an array literal is the easiest way to create a JavaScript Array.

Syntax:
const array_name = [item1, item2, ...];

 const bike = ["pulser", "apache", "duke"]; 
Enter fullscreen mode Exit fullscreen mode

Converting an Array to a String

The JavaScript method toString() converts an array to a string of (comma separated) array values.


const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
Enter fullscreen mode Exit fullscreen mode

Arrays are Objects

Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.

But, JavaScript arrays are best described as arrays.

Arrays use numbers to access its "elements". In this example, person[0] returns John:

 const person = ["John", "Doe", 46]; 
Enter fullscreen mode Exit fullscreen mode

Array Elements Can Be Objects

JavaScript variables can be objects. Arrays are special kinds of objects.

Because of this, you can have variables of different types in the same Array.

You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array:

myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myCars;
Enter fullscreen mode Exit fullscreen mode

Looping Array Elements

One way to loop through an array, is using a for loop:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fLen = fruits.length;

let text = "<ul>";
for (let i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>"; 
Enter fullscreen mode Exit fullscreen mode

Adding Array Elements

The easiest way to add a new element to an array is using the push() method:

const fruits = ["Banana", "Orange", "Apple"];
fruits.push("Lemon");  // Adds a new element (Lemon) to fruits 
Enter fullscreen mode Exit fullscreen mode

Associative Arrays

Many programming languages support arrays with named indexes.

Arrays with named indexes are called associative arrays (or hashes).

JavaScript does not support arrays with named indexes.

In JavaScript, arrays always use numbered indexes.

 const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
person.length;    // Will return 3
person[0];        // Will return "John" 

Enter fullscreen mode Exit fullscreen mode

When to Use Arrays. When to use Objects.

JavaScript does not support associative arrays.
You should use objects when you want the element names to be strings (text).
You should use arrays when you want the element names to be numbers.
Enter fullscreen mode Exit fullscreen mode

Nested Arrays and Objects

Values in objects can be arrays, and values in arrays can be objects:

const myObj = {
  name: "John",
  age: 30,
  cars: [
    {name:"Ford", models:["Fiesta", "Focus", "Mustang"]},
    {name:"BMW", models:["320", "X3", "X5"]},
    {name:"Fiat", models:["500", "Panda"]}
  ]
}
Enter fullscreen mode Exit fullscreen mode

Basic Array Methods
Array length
Array toString()
Array at()
Array join()
Array pop()
Array push()
Array shift()
Array unshift()
Array isArray()
Array delete()
Array concat()
Array copyWithin()
Array flat()
Array slice()
Array splice()
Array toSpliced()
Array some()
Array reduce()
Array map()
Array every()
Array flatMap()
Array filter()
Array findindex()
Array find()
Array fill()
Array forEach()
Array sort()
Array concat()
Array includes()
Array reverse()

Refrence
https://www.w3schools.com/js/js_arrays.asp
https://www.geeksforgeeks.org/javascript/best-known-javascript-array-methods/

Top comments (0)