Skip to content
the poor coder

array in js

Poor Man
Published
Reading time
3 min read
On this page

Array in JavaScript#

Arrays are a type of object in JavaScript that can store multiple values in a single variable. In other words, it is a collection of data items of the same type stored in contiguous memory locations. Each item in an array is called an element and is accessed by its index number.

Creating an Array#

To create an array, we can use the array literal notation, which is a comma-separated list of values enclosed in square brackets:

Text

var fruits = ["apple", "banana", "orange"];

We can also create an array using the Array() constructor:

Text

var cars = new Array("BMW", "Mercedes", "Audi");

Note that the Array() constructor can also take a single argument representing the length of the array to be created.

Accessing Array Elements#

We can access the elements of an array using their index numbers. The first element has an index of 0, the second element has an index of 1, and so on. We can also use negative index numbers to access elements from the end of the array.

Text

var fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // Output: "apple"
console.log(fruits[1]); // Output: "banana"
console.log(fruits[2]); // Output: "orange"
console.log(fruits[-1]); // Output: undefined
console.log(fruits[3]); // Output: undefined
console.log(fruits[fruits.length - 1]); // Output: "orange"

Array Methods#

JavaScript provides many methods for working with arrays. Some of the commonly used methods are:

  • push(): adds one or more elements to the end of an array
  • pop(): removes the last element from an array and returns it
  • shift(): removes the first element from an array and returns it
  • unshift(): adds one or more elements to the beginning of an array
  • slice(): returns a new array containing a portion of the original array
  • splice(): removes or replaces existing elements and/or adds new elements to an array

Here's an example of using some of these methods:

Text

var fruits = ["apple", "banana", "orange"];
console.log(fruits.length); // Output: 3

fruits.push("grape");
console.log(fruits); // Output: ["apple", "banana", "orange", "grape"]
console.log(fruits.length); // Output: 4

var lastFruit = fruits.pop();
console.log(lastFruit); // Output: "grape"
console.log(fruits); // Output: ["apple", "banana", "orange"]
console.log(fruits.length); // Output: 3

var firstFruit = fruits.shift();
console.log(firstFruit); // Output: "apple"
console.log(fruits); // Output: ["banana", "orange"]
console.log(fruits.length); // Output: 2

fruits.unshift("kiwi", "pear");
console.log(fruits); // Output: ["kiwi", "pear", "banana", "orange"]
console.log(fruits.length); // Output: 4

var citrusFruits = fruits.slice(2);
console.log(citrusFruits); // Output: ["banana", "orange"]
console.log(fruits); // Output: ["kiwi", "pear", "banana", "orange"]

var removedFruits = fruits.splice(1, 2, "cherry", "pineapple");
console.log(removedFruits); // Output: ["pear", "banana"]
console.log(fruits); // Output: ["kiwi", "cherry", "pineapple", "orange"]
console.log(fruits.length); // Output: 4

These are just a few of the many methods available for working with arrays in JavaScript. Knowing how to use them can make your code more efficient and easier to read.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC