;

Array In Javascript


Tutorialsrack 20/08/2022 Jquery Javascript

In this article, you’ll learn about Array in Javascript and how to use an array and its method.

Array in Javascript

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:

  1. A javascript array can hold multiple types of elements. For example, an array can store different types of elements such as integer type, boolean type, string type, and so on.
  2. The size of the array is dynamic. In other words, we don’t need to define the size of the array.

Creating an Array

In javascript, there are two ways to create an array:

  1. Using an array literal notation([])
  2. Using an Array constructor

1. Create an array using an Array Literal

This is the easiest way to create an array is by using an array literal []. For example,

Example: Create an array using an Array Literal
const arr = ["C#", "HTML", "Python", "Javascript", "Java"];

2. Create an array using an Array Constructor

You can also create an array using the Array() constructor. For example,

Example: Create an array using an Array Constructor
const arr = new Array("C#", "HTML", "Python", "Javascript", "Java");

Here are some more examples to create an array using the Array constructor:

Examples: 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");

Here are more examples of arrays:

Example
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,

Example
const mixedDataArray = [
    {'Language': 'Java'},
    [1, 2 ,3],
    function hello() { console.log('hello World!')}
];

Get an Array Length

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,

Example - Get an Array Length
const arr = ["C#", "HTML", "Python", "Javascript", "Java"];
 
console.log('Length of an Array: ', arr.length);
// Output => Length of an Array: 5

Access Elements of an Array

In Javascript, you can access elements of an array using indices like 0, 1, 2…so on. For example,

Example: Access Elements of an Array
const arr = ["C#", "HTML", "Python", "Javascript", "Java"];
 
// first element
console.log(arr[0]);  // "C#"
 
// second element
console.log(arr[3]); // "Javascript"

Javascript’s Array Methods

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

at()

This method returns the array item at the given index. This method accepts negative integers, which count back from the last item.

concat()

This method returns a new array which contains two or more merged arrays.

copywithin()

This method copies a sequence of array elements within an array and returns the modified array.

entries()

This method returns a new array iterator object that contains the key/value pairs for each index in an array.

every()

This method returns true if every element in the calling array satisfies the given condition.

fill()

This method replaces all elements in an array with a static value, from a start index (default 0) to an end index (default array.length). And it returns the modified array.

filter()

This method returns a new array containing all elements of the calling array for which the provided filtering function conditions return true.

find()

This method returns the value of the first element from the given array that fulfils the specified condition. And it returns undefined If no appropriate element is found.

findIndex()


This method returns the index value of the first element from the given array that fulfils the specified condition. And it returns -1 If no appropriate element is found.

findLast()

This method returns the value of the last element from the given array that fulfils the specified condition. And it returns undefined If no appropriate element is found.

findLastIndex()

This method returns the index value of the last element from the given array that fulfils the specified condition. And it returns -1 If no appropriate element is found.

flat()

This method returns a new array with all sub-array elements merged into it recursively till the specified depth.

flatMap()

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.

forEach()

This method invokes a function for each element in the calling array.

from()

This method creates a new array carrying the exact copy of another array element.

includes()

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 false.

indexOf()

This method searches the specified element in the given array and returns the index of the first match.

isArray()

This method checks if the passed value is an array or not.

join()

This method joins the elements of an array and returns as a string.

keys()

This method returns a new array iterator that contains the keys for each index in the calling array.

lastIndexOf()

This method searches the specified element in the given array and returns the index of the last match element.

map()

This method calls the specified function for each element in the array and returns the new array.

of()

This method creates a new array from a variable number of arguments, holding any type of argument.

pop()

This method removes and returns the last element of an array. It returns undefined if the array is empty.

push()

This method adds one or more elements to the end of an array. And return the length of the new array.

reduce(function, initial)

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.

reduceRight()

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.

reverse()

This method is used to reverse the elements of the given array.

shift()

This method removes and returns the first element of an array. It returns undefined if the array is empty.

slice()

This method is used to return a new array containing the copy of the portion of an array into a new array object.

some()

This method is used to determine if any element of the array passes the test of the provided testing function.

sort()

This method is used to return the element of the given array in sorted order.

splice()

This method is used to add/remove elements to/from the given array.

toLocaleString()

This method is used to return a string containing all the elements of a specified array.

toString()

This method is used to convert the elements of a specified array into string form, without affecting the original array.

unshift()

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.

values()

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!


Related Posts



Comments

Recent Posts
Tags