JavaScript Array Methods()...1

JavaScript Array Methods()...1

·

4 min read

Methods in this article:

  • .concat(),
  • .copyWithin(),
  • .entries(),
  • .every()

Hellooo and Welcome to article #1 in the series JavaScript Array Methods(), In this article, I am going to explain the basics of some array methods in Javascript(syntax, purpose, as well as example snippets),

So let's get started

.concat()

  • If you want to merge two or more sets in JavaScript, you can do it in two ways, first by using loops that we are not going to cover in this article and second by using concat method.

Syntax

 array1.concat(array2, array3, ...);

Example Snippet

let firstArray = [1, 2, 3, 4];
let secondArray = ["a", "b", "c"];
firstArray.concat(secondArray); // [1, 2, 3, 4, "a", "b", "c"]

.copyWithin()

  • This method is used to copy one or more elements from an array and paste them to another location in the same array.

  • It overwrites the original array

Syntax

arrayName.copyWithin(insertIndex, copyFromIndex, copyToIndex);
  • insertIndex- Index position where to copy elements usually known as 'target index',
  • copyFromIndex- Index position Where to start copying elements from,
  • copyToIndex- Index position up to which to copy items.

Example Snippet

let colors = ["Black", "Blue", "Yellow", "Red", "Green"];
colors.copyWithin(3, 2, 4); // ["Black", "Blue", "Yellow", "Yellow", "Red"]

.entries()

  • It returns an array iterator object that contains the pairs of elements and their index from the original array.

  • It doesn't alter the original array

Syntax

arrayName.entries();
  • It does not take arguments

Example Snippet

let colors = ["Black", "Blue", "Yellow", "Red", "Green"];
let arr = colors.entries(); 
for (x of arr){
 console.log(x);
} 
//[0, "Black"]
//[1, "Blue"]
//[2, "Yellow"]
//[3, "Red"]
//[4, "Green"]

.every()

  • This method iterates over all the elements and executes a function, and returns true if and only if the function returns true for all elements otherwise it returns false(even if a single element results in a false).
  • It doesn't alter the original array,

Syntax

array.every(function(currentElement, currentIndex, arrayItself), thisValue);
  1. function(currentElement, currentIndex, arrayItself)- (required) this is the function, this method executes on all elements,

    • currentElement- (required) value of current element being processed,

    • currentIndex- (optional) index of current element being processed,

    • arrayItself- (optional) array the element belongs to or the array this method is applied on.

  2. thisValue- (optional) A value to be passed to the method as a this value to the function.

    • "undefined" is the default value of its thisValue, if you omit this parameter, undefined will be passed.

Example Snippet

let numbers= [1, 10, 4, 23, 16, 19, 21, 7, 3];

console.log(numbers.every(function(num){
return num <= 25;
}));  //true

That's it for now, these are the four array methods out of many, more of them are coming soon stay tuned, This was article #4.1 as a part of the series JavascriptArray Methods().