Arrays

AkashAkash
5 min read

TABLE CONTENT:

  • WHAT ARE ARRAYS

  • ARRAY STRUCTURE

  • SYNTAX TO DEFINE ARRAY IN JAVASCRIPT

  • ARRAYS PROPERTIES

  • Conclusion

WHAT ARE ARRAYS:

Arrays are non-primitive data-types which are used to store multiple values .In Java-script arrays you can same type of values in a single array as well as mixed type of values depend on situation.

ARRAY STRUCTURE:

You can consider array as a box inside which you can store multiple boxes containing data. Each box in an array is called the element of array. Every element of an array is given a identity ,this identity is called as array indexing . Indexing of array is start with 0. The first element of an array is called as array[0],second element is called as array[1], and similarly other element.

SYNTAX TO DEFINE ARRAY IN JAVASCRIPT:

With the help of let or const keyword we can define an array in java-script. If you want that your array remains constant then you can use the const keyword but if you want some processing in your array then you can use let keyword.

ARRAY PROPERTIES:

There are many types of array properties , Here we are discussing few array properties.

1-MODIFYING ARRAY ELEMENTS

If you want to modify any array element then you can modify in such a way.

let city=["Kanpur" , "Delhi" , "Mumbai" , "Bangalore"];

city[1]="Chennai";

Then the upgrade array of cities is:

city=["Kanpur" , "Delhi" , "Mumbai" , "Bangalore"];

INPUT:

OUTPUT:

2-LENGTH OF AN ARRAY:

let city=["Kanpur" , "Delhi" , "Mumbai" , "Bangalore"];

With the help of length function you can find the number of elements in an array:

console.log(city.length) will give the output 4…

3-push:

With the help of push function you can add an element at the end of the array.

Input:

Output:

4-pop()

With the help of this function you can remove an element from the end of the array.

INPUT:

Output:

5-Shift

With the help of this method you can remove an array element from start or you can remove the element which is present on index 0.

Input:

Output:

6-Unshift()

With the help of this method you can add an element at very start or at 0th index.

Input:

Output:

7-Include

With the help of this array property you can find whether any certain element exists in any array or not . It will return True or False.

Input:

Output:

8-Index of()

This array property return the index of any element present in the array. If element do not exist inside the array then it return -1.

Input:

Output:

9-Sorting:

With the help of this property we can sort any array. If we pass any array of string then it will sort that array according to alphabetical order. If we pass any array of number then it will also sort that array according to alphanumeric character. So , to sort an array of numbers we have to little upgrade the property. So to sort the array of number If you want to sort them in ascending order then you have to use Sort((a , b)=>a-b) and if you want to sort them in descending order , you have to use Sort((a , b)=>b-a)

Input:

Output:

10-REVERSE

This property reverse the array elements

Input:

Output:

ARRAY PROPERTIES WITH THE HELP OF AN EXAMPLE:

suppose you are an library owner and you have to maintain the information of students which are enrolled in your library. Then, with the help of that array and array properties you can easily maintain the record of students.

Suppose you want to add any new student or remove any student in your library then you can use #push or #pop methods.

In your library, there are reserved as well as unreserved seats , one day any student leaving his reserved seat at the same time a new student want that seat then with the help of #modify function you can do that task.

Some day you want to find total number of student enrolled in your library with the help of #length function , you can find the total strength .

Similarly, if you want to remove an student from start you can use #shift method to do that task or you want to add an student at start of the list you can use #unshift keyword to do so.

If any day any parent come to give his/her child tiffin box and he/she does not know that in which seat his/her student sit so you can help them by using #Indexof() property . This property will return the seat number of the student (In array it will give the index of that element.).If no student present of that name then it will return the -1.

If someday in you fees register you want to sort the student in order to their alphabetical order then with the help of #sort property you can do that task.

If you want to reverse the order of student then you can use the #reverse property to do that task.

CONCLUSION:

With help of these properties you can do some modification in your arrays.

These are all the simple and basic level property of array in Java script . In my further blogs I will write about advanced properties of array which will help you to grow better. Thanks for remaining with this blog till last. Hope this blog help you to understand these array properties better.

10
Subscribe to my newsletter

Read articles from Akash directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akash
Akash