Array methods

__khojiakbar__

_Khojiakbar_

Posted on June 8, 2024

Array methods

BASIC ARRAY METHODS

  1. Array.length
// Array length shows how many elements exist in the array

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
let result = fruits.length;

console.log(result);
Enter fullscreen mode Exit fullscreen mode
  1. Array.toString()
// The JavaScript method toString() converts an array to a string of (comma separated) array values.

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
let result = fruits.toString();

console.log(result);
Enter fullscreen mode Exit fullscreen mode
  1. Array.at(index)
// at() returns the element at the specified index

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
let result = fruits.at(2);

console.log(result);
Enter fullscreen mode Exit fullscreen mode
  1. Array.join('whatever')
// join() joins the elements of an array into a string and joins with whatever we insert inside (join()) parentheses

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
let result = fruits.join('-')

console.log(result); // => apple-banana-cherry-date-elderberry
Enter fullscreen mode Exit fullscreen mode
  1. Array.pop()
// pop() removes the last element from an array and returns that element

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];

let result = fruits.pop()

console.log(result); // => elderberry
console.log(fruits); // => ['apple', 'banana', 'cherry', 'date'];
Enter fullscreen mode Exit fullscreen mode
  1. Array.push()
// Array.push() adds from the end and shows the length

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
let result = fruits.push('kiwi');

console.log(result); // => 6 ! returns the length
console.log(fruits); // => ['apple', 'banana', 'cherry', 'date', 'elderberry', 'kiwi']
Enter fullscreen mode Exit fullscreen mode
  1. Array.shift()
// shift() deletes from the beginning and returns the deleted element

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];

let result = fruits.shift();

console.log(result); // => apple  
Enter fullscreen mode Exit fullscreen mode
  1. Array.unshift()
// unshift() adds from the beginning and returns the new length

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
let result = fruits.unshift('pear');

console.log(result); // => apple    
console.log(fruits); // => ['pear', 'apple', 'banana', 'cherry', 'date', 'elderberry']
Enter fullscreen mode Exit fullscreen mode
  1. delete array[index]
// delete leaves empty holes in the array therefore use push() or pop()

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];

delete fruits[1];

console.log(fruits); // => ['apple', empty, 'banana', 'cherry', 'date', 'elderberry']
Enter fullscreen mode Exit fullscreen mode
  1. concat()
// The concat() method creates a new array by merging (concatenating) existing arrays:

const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];

const myChildren = myGirls.concat(myBoys);
Enter fullscreen mode Exit fullscreen mode
  1. array.flat()
// The flat() method creates a new array with sub-array elements concatenated to a specified depth.

const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();

console.log(newArr); // => [1,2,3,4,5,6]
Enter fullscreen mode Exit fullscreen mode
  1. array.splice()
let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
fruits.splice(2, 0, 'kiwi') // => ['apple', 'banana', 'kiwi', 'date', 'elderberry'];

console.log(fruits);
Enter fullscreen mode Exit fullscreen mode
  1. array.slice()

let fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
let result = fruits.slice(2, 3) // => ['cherry']

console.log(result);
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
__khojiakbar__
_Khojiakbar_

Posted on June 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related