Arrays In JavaScript (A quick walkthrough)
This article will briefly explain you about java script arrays
Why Do we use Arrays?
If you have a list of flavours, storing the flavours in single variables could look like this:
let f1 = "Vanilla";
let f2 = "Strawberry";
let f3 = "Chocolate";
However, what if you want to loop through the flavours and find a specific one? And what if you had not 3 flavours, but 300?
The solution is an array!
Creating An array
There are two ways to create an array in javascript
1. Using an array literal
const flavours = [ "Vanilla", "Strawberry", "Chocolate"];
2.Using the JavaScript Keyword new
const flavours = new Array("Vanilla", "Strawberry", "Chocolate");
We can also create an array using javascript new
keyword but it is not recommended
as it provides unexpected results while declaring an array with single element.
Arrays in Javascript Vs Arrays in other languages
Array declaration in Java
int[] arr = new int[5]{1,2,3,4,5};
Array declaration in javascript
const person = ["John", "Doe", 46];
As you may have noticed in the above array declaration examples,
you can see in other languages(JAVA or any other similar languages) ,
datatypes of all the array elements need to be same but in javascript
datatypes of array elements can be different.
Accessing Array Elements
You access an array element by referring to the index
number:
const person = ["John", "Doe", 46];
console.log(person[0]);
output
John
Some commonly used array methods
Method | Description |
JavaScript Array length | Returns the number of elements in an array |
JavaScript Array reverse() | Returns the array in reverse order |
JavaScript Array sort() | Sorts the elements of an array in specific order |
JavaScript Array fill() | Returns array by filling elements with given value |
Javascript Array join() | Concatenates the array elements to a string |
Javascript Array toString() | Returns the string representation of an array |
JavaScript Array pop() | Removes and returns the last array element |
JavaScript Array shift() | Removes and returns the first array element |
JavaScript Array push() | Adds elements to end of array & returns its length |
JavaScript Array unshift() | Adds elements to start of array and returns length |
JavaScript Array concat() | Returns array by merging given value and/or arrays |
JavaScript Array splice() | Returns an array by changing its elements in place |
JavaScript Array lastIndexOf() | Returns the last index of occurrence of an element |
JavaScript Array indexOf() | Returns the first index of occurrence of element |
JavaScript Array of() Method | Creates a new Array instance from given arguments |
JavaScript Array slice() | Returns a shallow copy of a portion of an array |
JavaScript Array findIndex() | Returns index of element that satisfies condition |
JavaScript Array find() | Returns first element that satisfies a condition |
JavaScript Array includes() | Checks if a value exists in an array |
Javascript Array reduce() | Reduces array to single value from left to right |
Javascript Array isArray() | Checks if the given value is a JavaScript Array |
Javascript Array filter() | Returns array by filtering elements on given test |
JavaScript Array map() | Returns array by mapping elements using given func |
Javascript Array forEach() | Executes the given function on array elements |
Javascript Array some() | Tests if any element passes given test function |
End
I hope you find this article helpful ๐
Thank you for your time and patience โค๏ธ
To explore more,
you can go to this link - Mozilla Documentation for arrays