Javascript and  My 10 Favourite Array Method:

Javascript and My 10 Favourite Array Method:

·

4 min read

1. What is Javascript?

In general terms, we all know that JavaScript is a programming language which is used to make a website interactive and dynamic (ability to change things at runtime). Just assume things that

HTML builds the structure/skeleton with text, images, and buttons, while CSS (Cascading Style Sheets) is paint and decorating, which makes things look nice using colors and layouts. Whereas JavaScript is the brain which makes things work or happen when we interact with the page.


2. Array Methods.

Array is collection of items(data) that are stored in a single variable(memory location).

Array methods are built-in-function(functions that is already provided by programming language) can be used to perform task.


Lets understand array methods with some scenario based explaination:

lets assume we are a milkman and we have to keep records of amount of milk is delivered to each house daily and for keeping these records we have an array .we have to manage these deliveries

  1. Push() :

The push() method is used to add one or more items to the end of an array. eg:(At the end of the day, we received a new customer request for a delivery of 2 liters of milk. We need to make sure this order is added to our list so it can be processed accordingly.)

  1. Pop():

The pop() method is used to remove last item of an array . eg:(One of the customeres has canceled their milk order so we need to remove their delivery form the list to keep aur records updated).

  1. Shift()

The shift() method is used to remove first item of an array. eg:(The First coustomer from our route doesn’t need milk today so we need to remove their delivery from the start of the list to keep it accurate).

  1. Unshift():

The unshift() method is use to add one or more item to the beginning of an array. eg:(A new customer at the start of our route needs 10 liters of milk so we need to add their delivery to the beginning of the list ).

  1. Slice():

The slice() method is used to extract a section of an array and return it as new array. eg:(we need to get the deliveries of the middle house which are located at from index 1 to 3 , so we can slice the array)

  1. Includes():

The includes() method is used to check if an array contains any specific item.it returns true if it is found otherwise false. eg:(We need to check if any house received exactly 10 liters of milk today. To do this, we can use the includes() method to see if the array contains the value 10)

  1. Sort():

the sort() method sorts the items of an array in place (modifies the original array). By default, it sorts the items as strings in ascending order. eg(We need to sort the deliveries in ascending order to easily find the house that received the smallest amount of milk. This way, the delivery with the least amount will be at the beginning of the list).

  1. reverse():

The reverse() method is used to reverse the order of the items in an array. eg:(We need to reverse the order of the deliveries so that they appear in descending order. This way, we can start with the largest deliveries and finish with the smallest ones).

  1. Indexof():

the indexOf() method is used to find the first index of a specified item in an array. It returns the index of the first occurrence of the item, or -1 if the element is not found. eg:(We need to find the index of the delivery where the milk amount is 3 liters).

  1. reduce():

    The reduce() method in JavaScript is used to combine all items of an array into a single value by applying a function to each item. eg:(We need to calculate the total amount of milk delivered for the day).