Array & it's methods ( part-1 )

Array & it's methods ( part-1 )

ยท

5 min read

Array:

An Array is an object which stores a collection of multiple items in a single variable. Javascript arrays can contain data (items) of different data types. In an array, indexing starts with 0 and ends with Array length minus 1.

How to create an Array:

  1. Creating an array using an array literal :
let socialApps = [ "facebook", "instagram", "whatsapp" ];
  1. Creating an array using a "new " keyword :
let socialApps = new Array( "facebook", "instagram", "whatsapp" )

Array Methods:

concat() :

  1. This method is used to join two or more arrays.

  2. It returns a new array, containing the joined arrays.

  3. It does not modify the existing array.

# Syntax :

arr1.concat(  arr2 );

</> Code eg :

let arr1 = [ "html", "css" ];
let arr2 = [ "javascript" ];
let newArray = arr1.concat( arr2 );
console.log(newArray);
๐Ÿ—ณ๏ธ output :

            [ "html", "css", "javascript" ]

copywithin() :

  1. This method is used to copy array elements in the same array and paste them at a different position in the array itself.

  2. It modifies the existing array.๐Ÿ”บ

# Syntax :

arrayName.copywithin( target, start, end );

//in more detail
arrayName.copywithin( target-paste-index, start-copy-index, end-copy-index );

Here target-index is the index number that tells where the copied elements should get pasted from. And coming to the start-copy-index and end-copy-indexes. They tell, where the elements should be copied from and up to which position they should be copied. Confused? ๐Ÿ˜• clearly explained below...๐Ÿ‘‡

</> Code eg :

let arr1 = [ 'M', 'E', 'R', 'N' ];
arr1.copywithin(2,0,2);
console.log(arr1);
๐Ÿ—ณ๏ธ output :

             [ 'M', 'E', 'M', 'E' ]

๐Ÿ“š Explanation :

๐Ÿ’กNote :

  • arr1.copywithin ( 2 ) => here "target = 2" & "start = 0" & "end = lastIndex"

  • start & end indexes are optional.

  • If the start & end are skipped then default start = 0 and default end = lastIndex

  • โญ In most of the cases end index is excluded.

entries() :

  1. This method returns an array-iterator with key-value pairs of the array. Here the key is the index and the value is an array item.

  2. It does not modify the existing array.

# Syntax :

arrayName.entries();

</> Code eg :

let arr1 = [ 'M', 'E', 'R', 'N' ];
let newArray = arr1.entries();
for (let i of newArray){
console.log(i);
}
๐Ÿ—ณ๏ธ output :
            [ 0, 'M' ]
            [ 1, 'E' ]
            [ 2, 'R' ]
            [ 3, 'N' ]

every() :

  1. This method returns a boolean value either "true" or "false". It is used to check whether every element in the array satisfies the given condition or not, which is (the condition is) given inside the function.

  2. It returns "true" if every element satisfies the condition, if not it returns "false".

  3. It does not modify the existing array.

# Syntax :

arrayName.every( callback-function ); //or
arrayName.every( (element) => { condition } ); //or
arrayName.every( function(element) { condition } );

</> Code eg :

let arr1 = [ 2, 4, 6, 8 ];
console.log(arr1.every(checkEven));

function checkEven(element){
         return element%2 == 0;
}
๐Ÿ—ณ๏ธ output :
            true

fill() :

  1. This method is used to change the array elements in an array. This method copies a value and pastes it at different positions in the array.

  2. It fills the array with the given fill value between the index positions mentioned.

  3. It modifies the existing array.๐Ÿ”บ

# Syntax :

arrayName.fill( value, start, end );

//in more detail
arrayName.fill( value-copy, start-paste-index, end-paste-index );

Here value-copy is the "value" that tells which value to be copied. And coming to the start-paste-index and end-paste-indexes. They tell, where the elements should be pasted from and up to which position they should be pasted. Clearly explained below...๐Ÿ‘‡

</> Code eg :

let arr1 = [ 'M', 'E', 'R', 'N' ];
arr1.fill(2,0,2);
console.log(arr1);
๐Ÿ—ณ๏ธ output :

             [ 2, 2, 'R', 'N' ]

๐Ÿ“š Explanation :

๐Ÿ’กNote :

  • arr1.fill ( 2 ) => here "value = 2" & "start = 0" & "end = lastIndex". which means it fills the total array.

  • start & end indexes are optional.

  • If the start & end are skipped then default start = 0 and default end = lastIndex

  • value can be of any datatype like number, strings any data type.

  • โญ In most of the cases end index is excluded.

Summary :

For your clear understanding, I made a table๐Ÿ‘‡ that clear's all your doubts and confusion.

Array MethodSyntaxModifies original Array
concat( )arr1.concat ( arr2, arr3, .... )No
copywithin( )arr1. copywithin ( target, start, end )Yes
entries( )arr1.entries( )No
every( )arr1.every( callback function )No
fill( )arr1.fill( value, start, end )Yes

Thank you for reading.

๐Ÿ˜… to be continued....... in Array & its methods part-2

ย