JavaScript Array Methods and Properties

date
May 9, 2024
slug
js-array-methods
status
Published
tags
js
javascript
summary
Here's a brief description and example for each of the listed array methods
type
Post
 
  1. new Array: Creates a new Array.
    1. let newArray = new Array();
  1. at(): Returns an indexed element of an array.
    1. const fruits = ['apple', 'banana', 'cherry']; console.log(fruits.at(1)); // Output: banana
  1. concat(): Joins arrays and returns an array with the joined arrays.
    1. const array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const newArray = array1.concat(array2);
  1. constructor: Returns the function that created the Array prototype.
    1. const arr = []; console.log(arr.constructor === Array); // Output: true
  1. copyWithin(): Copies array elements within the array, to and from specified positions.
    1. const array = ['a', 'b', 'c', 'd', 'e']; array.copyWithin(0, 3, 4); // Changes array to ['d', 'b', 'c', 'd', 'e']
  1. entries(): Returns a key/value pair Array Iteration Object.
    1. const fruits = ['apple', 'banana', 'cherry']; const iterator = fruits.entries();
  1. every(): Checks if every element in an array passes a test.
    1. const ages = [32, 33, 16, 40]; const isAdult = (age) => age >= 18; console.log(ages.every(isAdult)); // Output: false
  1. fill(): Fills the elements in an array with a static value.
    1. const array = [1, 2, 3, 4]; array.fill(0, 2, 4); // Changes array to [1, 2, 0, 0]
  1. filter(): Creates a new array with every element in an array that passes a test.
    1. const numbers = [10, 20, 30, 40, 50]; const result = numbers.filter(number => number > 30);
  1. find(): Returns the value of the first element in an array that passes a test.
    1. const array = [5, 12, 8, 130, 44]; const found = array.find(element => element > 10);
  1. findIndex(): Returns the index of the first element in an array that passes a test.
    1. const array = [5, 12, 8, 130, 44]; const index = array.findIndex(element => element > 10);
  1. findLast(): Returns the value of the last element in an array that passes a test.
    1. const array = [5, 12, 8, 130, 44]; const found = array.findLast(element => element > 10);
  1. findLastIndex(): Returns the index of the last element in an array that passes a test.
    1. const array = [5, 12, 8, 130, 44]; const index = array.findLastIndex(element => element > 10);
  1. flat(): Concatenates sub-array elements.
    1. const arr = [1, 2, [3, 4]]; const flatArr = arr.flat();
  1. flatMap(): Maps all array elements and creates a new flat array.
    1. const arr = [1, 2, 3]; const mapped = arr.flatMap(x => [x * 2]);
  1. forEach(): Calls a function for each array element.
    1. const array = [1, 2, 3]; array.forEach(element => console.log(element));
  1. from(): Creates an array from an object.
    1. const arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3 }; const array = Array.from(arrayLike);
  1. includes(): Checks if an array contains the specified element.
    1. const array = [1, 2, 3]; const isIncluded = array.includes(2);
  1. indexOf(): Searches the array for an element and returns its position.
    1. const array = [2, 9, 9]; const index = array.indexOf(9);
  1. isArray(): Checks whether an object is an array.
    1. const array = [1, 2, 3]; const isArray = Array.isArray(array);
  1. join(): Joins all elements of an array into a string.
    1. const array = ['Hello', 'world!']; const joined = array.join(' ');
  1. keys(): Returns an Array Iteration Object containing the keys of the original array.
    1. const array = ['a', 'b', 'c']; const iterator = array.keys();
  1. lastIndexOf(): Searches the array for an element, starting at the end, and returns its position.
    1. const array = [2, 5, 9, 2]; const index = array.lastIndexOf(2);
  1. length: Sets or returns the number of elements in an array.
    1. const array = [1, 2, 3]; const length = array.length;
  1. map(): Creates a new array with the result of calling a function for each array element.
    1. const numbers = [1, 4, 9]; const roots = numbers.map(Math.sqrt);
  1. of(): Creates an array from a number of arguments.
    1. const array = Array.of(1, 2, 3);
  1. pop(): Removes the last element of an array, and returns that element.
    1. const array = [1, 2, 3]; const lastElement = array.pop();
  1. prototype: Allows you to add properties and methods to an Array object.
    1. Array.prototype.myCustomFunction = function() { // Custom function logic };
  1. push(): Adds new elements to the end of an array, and returns the new length.
    1. const array = [1, 2, 3]; const newLength = array.push(4, 5);
  1. reduce(): Reduces the values of an array to a single value (going left-to-right).
    1. const array = [1, 2, 3, 4]; const sum = array.reduce((accumulator, currentValue) => accumulator + currentValue);
  1. reduceRight(): Reduces the values of an array to a single value (going right-to-left).
    1. const array = [1, 2, 3, 4]; const sum = array.reduceRight((accumulator, currentValue) => accumulator + currentValue);
  1. reverse(): Reverses the order of the elements in an array.
    1. const array = ['one', 'two', 'three']; array.reverse();
  1. shift(): Removes the first element of an array, and returns that element.
    1. const array = [1, 2, 3]; const firstElement = array.shift();
  1. slice(): Selects a part of an array, and returns the new array.
    1. const array = [1, 2, 3, 4, 5]; const sliced = array.slice(2, 4);
  1. some(): Checks if any of the elements in an array pass a test.
    1. const array = [1, 2, 3, 4, 5]; const hasEvenNumber = array.some(element => element % 2 === 0);
  1. sort(): Sorts the elements of an array.
    1. const array = [2, 1, 3]; array.sort();
  1. splice(): Adds or removes array elements.
    1. const array = ['a', 'b', 'c', 'd']; array.splice(2, 0, 'x', 'y');
  1. toReversed(): Reverses the order of array elements (to a new array).
    1. const array = [1, 2, 3]; const reversedArray = array.slice().reverse();
  1. toSorted(): Sorts the elements of an array (to a new array).
    1. const array = [3, 2, 1]; const sortedArray = array.slice().sort();
  1. toSpliced(): Adds or removes array elements (to a new array).
    1. const array = ['a', 'b', 'c']; const newArray = array.slice().splice(1, 1);
  1. toString(): Converts an array to a string, and returns the result.
    1. const array = ['a', 'b', 'c']; const string = array.toString();
  1. unshift(): Adds new elements to the beginning of an array, and returns the new length.
    1. const array = [1, 2, 3]; const newLength = array.unshift(0);
  1. valueOf(): Returns the primitive value of an array.
    1. const array = [1, 2, 3]; const primitiveValue = array.valueOf();
  1. with(): Returns a new array with updated elements. (Note: There is no with() method for arrays in JavaScript.)
இந்த உரையைப் பற்றிய எந்த கேள்விகள் உள்ளன, என்னுடைய தொடர்புக்கு தொடரவும்.