DEV Community

Deva I
Deva I

Posted on

Details About the Basic Array Methods

In the previous blog I was post the basic array methods.In this blog discuss the detail about the array methods.

Array length:

◾The length method returns the count or size of an array.

EXAMPLE:

🔹In the array have 5 elements,so its returns the length 5.

OUTPUT:

Array toString()

◾The tostring()method returns the elements of an array to a separated string with a comma.

EXAMPLE:

🔹This method converts a every single Mobilebrand into an one single array seperate a comma.

OUTPUT:

Array at()

◾The at() method returns an indexed element from an array.

EXAMPLE:

🔹 Mobilebrand.at(3) returns a indexed element 3 'samsung'.

OUTPUT:

Array Join()
◾The join() method also joins all array elements into a string.

◾This acts like a tostring().

EXAMPLE:

🔹In this join() method we give a (+),so its create a array into a string joins with inbetween (+) element.

OUTPUT:

Array pop()

◾The pop() method removes the last element from an array.

EXAMPLE:

🔹pop() method removes the end element 'nothing' frome the array.

OUTPUT:

Array push()

◾The push() method adds a new element in the end of an array.

EXAMPLE:

🔹push() method adds a 'vivo' end of the array.

OUTPUT:

Array shift()

◾The shift() method removes the first array element and "shifts" all other elements to a starting index.

EXAMPLE:

🔹Shift() method removes first element 'realme'.

🔹The balance elements shifts the place of removed element.

OUTPUT:

Array unshift()

◾The unshift() method adds a new element in the beginning of an array and the other elements unshifts the older elements.

EXAMPLE:

🔹'oneplus' added to the beginning of the array.

OUTPUT:

Array.isArray()

◾The Array.isArray()used to finds array is really a array to returns a true.

EXAMPLE:

🔹Mobilebrand is array,so its returns a true.

OUTPUT:

Array delete()

delete()method returns a undefined.

◾Use the push and pop method instead of delete.

EXAMPLE:

🔹Mobilebrand[1] defines undefined.

OUTPUT:

Array concat()

◾The concat() method creates a new array to merging the existing arrays.

EXAMPLE:

🔹Concat merge the GroupB array to existing array GroupA to in a single array.

OUTPUT:

Array copy Within()

◾The copy Within() method copies array elements to another position in an array.

EXAMPLE:

OUTPUT:

Array flat()

◾The flat() method creates a new array and merge with sub-array.

EXAMPLE:

🔹flat method Merge the sub arrays with array
groups.

🔹Merge the sub array numbers in one array.

OUTPUT:

Array splice()

◾The splice() method can be used to add new items to an array.

EXAMPLE 1:

OUTPUT 1:

🔹The first parameter (2) defines the position where new elements should be added.

🔹dc and srh added index 2

🔹The second parameter (0) defines how many elements should be removed.

🔹Second value is 0 no value has been removed.

EXAMPLE 2:

🔹The first parameter (2) defines the position where new elements should be added.

🔹dc and srh added index 2

🔹The second parameter (1) defines how many elements should be removed.

🔹Second value is 1,so 'rcb' has been removed.

OUTPUT 2:

Array Slice()

◾The slice() method slices out a piece of an array into a new array:

EXAMPLE:

🔹Slice out a part of an array starting from array element 1 ("csk")

OUTPUT:

Array to Spliced()

◾It also removes the element from the array.

EXAMPLE:

🔹'0' defines the index and '2' defines the two elements.

🔹Elements removed starting from the index 0 to two elements.

OUTPUT:

Top comments (0)