Array-Javascript

Array-Javascript

Definition

An array is a collection of similar data elements stored at contiguous memory locations.

Syntax

// Using the javascript array literal
const array_name = [item1, item2, ...];  

//  Using the JavaScript new Keyword 
const array_name = new Array(item1,item2,...);

Why Use Arrays?

An array can hold many values under a single variable, and you can access the values by referring to an index number.

Advantages of using an array:

  • Arrays store multiple data of similar types with the same name.
  • It allows random access to elements.
  • As the array is of fixed size and stored in contiguous memory locations there is no memory shortage or overflow.
  • It is helpful to store any type of data with a fixed size.

Array Methods

1. length()

The length property returns or sets the number of elements in an array.

let items = ["item1", "item2", "item3", "item4"];
// find the length of the array
let len = items.length;
console.log(len);
// Output: 4

2. push()

The push() method adds elements to the end of the array.

let items = ["item1", "item2", "item3", "item4"];
// To add an element at the end of the array
items.push("item5");
console.log(items);
// Output: "item1", "item2", "item3", "item4", "item5"

3. pop()

The pop() method removes the last element from an array and returns that element.

let items = ["item1", "item2", "item3", "item4"];
// remove last element
let removed_element= items.pop();
console.log(items); // "item1", "item2", "item3", "item4"
console.log(removed_element);   // item4

4. sort()

The sort() method sorts the method of an array in a specific order.

let names = ["nitin", "bhawesh", "ankit", "vinod"];
// sort the array in ascending order
let sortedArray = names.sort();
console.log(names); // "ankit", "bhawesh", "nitin", "vinod"

5.shift()

The shift() method removes the first element from an array and returns that element.

let items = ["item1", "item2", "item3", "item4"];
// remove the first element and modify the original array 
let element = items.shift();
console.log(element);   // item1
console.log(items); // "item2", "item3", "item4"

6.unshift()

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

let languages = ["Java", "Python", "C"];

// add "JavaScript" and "typescript" at the beginning of the array
languages.unshift("JavaScript","typescript");
console.log(languages);

// Output: [ 'JavaScript','typescript','Java', 'Python', 'C' ]

7.slice()

The slice() method returns selected elements in an array, as a new array but it won't change the original array. This method selects from a given start, up to a (not inclusive) given end.

Syntax : array.slice(start, end)

let numbers = [2, 3, 5, 7, 11, 13, 17];

// create another array by slicing numbers from index 3 to 5
let newArray = numbers.slice(3, 6);
console.log(newArray);

// Output: [ 7, 11, 13 ]

8.splice()

The splice() method returns an array by changing (adding/removing) its elements in place.

let prime_numbers = [2, 3, 5, 7, 9, 11];

// replace 1 element from index 4 by 13
let removedElement = prime_numbers.splice(4, 1, 13);
console.log(removedElement);
console.log(prime_numbers);

// Output: [ 9 ]
//         [ 2, 3, 5, 7, 13, 11 ]