In this article, you’ll learn about Array in Javascript and how to use an array and its method.
In javascript, an array is an object, which is used to store an ordered list of values, and each value is called an element that can be accessed by an index.
In javascript, an array has the following characteristics:
integer
type, boolean
type, string
type, and so on.In javascript, there are two ways to create an array:
[]
)Array
constructorThis is the easiest way to create an array is by using an array literal []
. For example,
const arr = ["C#", "HTML", "Python", "Javascript", "Java"];
You can also create an array using the Array()
constructor. For example,
const arr = new Array("C#", "HTML", "Python", "Javascript", "Java");
Here are some more examples to create an array using the Array
constructor:
//Create an empty array
const arr1 = new Array();
//Create an array with initial size of 10
const arr2 = new Array(10);
//Create an array with single element
const arr3 = new Array('Red');
//Create an array with an 5 elements
const ar4 = new Array("C#", "HTML", "Python", "Javascript", "Java");
[]
to create an array.Here are more examples of arrays:
const emptyArray = [ ];
// array of numbers
const numberArray = [ 2, 4, 6, 8];
// array of strings
const stringArray = [ "C#", "HTML", "Python", "Javascript", "Java"];
// array with mixed data types
const mixedDataTypeArray = ["C#", "HTML", "Python", 1, true];
In Javascript Array, you can also store arrays, functions, and other objects inside an array as an element. For example,
const mixedDataArray = [
{'Language': 'Java'},
[1, 2 ,3],
function hello() { console.log('hello World!')}
];
In javascript, The length property of an object which is an instance of type Array sets or returns the number of elements in that array. For example,
const arr = ["C#", "HTML", "Python", "Javascript", "Java"];
console.log('Length of an Array: ', arr.length);
// Output => Length of an Array: 5
In Javascript, you can access elements of an array using indices like 0, 1, 2…so on. For example,
const arr = ["C#", "HTML", "Python", "Javascript", "Java"];
// first element
console.log(arr[0]); // "C#"
// second element
console.log(arr[3]); // "Javascript"
In JavaScript, there are various built-in array methods that make it easier to perform useful calculations on arrays.
Some of the most used JavaScript’s built-in array methods are as follows:
Methods |
Description |
This method returns the array item at the given index. This method accepts negative integers, which count back from the last item. |
|
This method returns a new array which contains two or more merged arrays. |
|
This method copies a sequence of array elements within an array and returns the modified array. |
|
This method returns a new array iterator object that contains the key/value pairs for each index in an array. |
|
This method returns true if every element in the calling array satisfies the given condition. |
|
This method replaces all elements in an array with a static value, from a start index (default |
|
This method returns a new array containing all elements of the calling array for which the provided filtering function conditions return |
|
This method returns the value of the first element from the given array that fulfils the specified condition. And it returns |
|
This method returns the index value of the first element from the given array that fulfils the specified condition. And it returns |
|
This method returns the value of the last element from the given array that fulfils the specified condition. And it returns |
|
This method returns the index value of the last element from the given array that fulfils the specified condition. And it returns |
|
This method returns a new array with all sub-array elements merged into it recursively till the specified depth. |
|
This method returns a new array formed by applying a given map function to each element of the array and then flattens the result into a new array. |
|
This method invokes a function for each element in the calling array. |
|
This method creates a new array carrying the exact copy of another array element. |
|
This method checks whether the given array contains the specified element or not. It returns true if an array contains specific elements, otherwise, it returns |
|
This method searches the specified element in the given array and returns the index of the first match. |
|
This method checks if the passed value is an array or not. |
|
This method joins the elements of an array and returns them as a string. |
|
This method returns a new array iterator that contains the keys for each index in the calling array. |
|
This method searches the specified element in the given array and returns the index of the last match element. |
|
This method calls the specified function for each element in the array and returns the new array. |
|
This method creates a new array from a variable number of arguments, holding any type of argument. |
|
This method removes and returns the last element of an array. It returns undefined if the array is empty. |
|
This method adds one or more elements to the end of an array. And return the length of the new array. |
|
This method executes a provided “reducer” callback function for each value of the array from left to right and reduces the array to a single value. |
|
This method executes a provided “reducer” callback function for each value of the array from right to left and reduces the array to a single value. |
|
This method is used to reverse the elements of the given array. |
|
This method removes and returns the first element of an array. It returns |
|
This method is used to return a new array containing the copy of the portion of an array into a new array object. |
|
This method is used to determine if any element of the array passes the test of the provided testing function. |
|
This method is used to return the element of the given array in sorted order. |
|
This method is used to add/remove elements to/from the given array. |
|
This method is used to return a string containing all the elements of a specified array. |
|
This method is used to convert the elements of a specified array into string form, without affecting the original array. |
|
This method is used to add one or more elements at the beginning of the given array and returns the new length of the array. |
|
This method is used to create a new iterator object carrying values for each index in the array. |
I hope this article will help you to understand Array in Javascript.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments