Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Arrays in JavaScript

Arrays in JavaScript

Introduction to Arrays in JavaScript

As we know objects allow us to store keyed collections of values. But sometimes we need an ordered collection, where we have to store 1st, 2nd, 3rd element and many more. For instance, consider we need to store a list of something: users’ data, goods data, and many more. It is not appropriate to use an object here, because it provides no methods to manage the order of elements. Objects are not created for such use. There exists a special data structure called Array, to store ordered collections. Arrays in JavaScript enables multiple values to be stored in a single variable. It stores the same kind of element collection sequential fixed-size.

Arrays in JavaScript are used to store an information set, but it is often more helpful for storing a set of variables of the same type. An array is a single variable in JavaScript that is used to store various elements. When we want to store a list of elements and access them through a single variable, it is often used. Unlike many other languages where an array is a various variable reference, there is a single variable in the Javascript Array that stores various elements. The object Arrays in JavaScript are a global object used in array building; which is high-level, list-like objects.

How does the array work in JavaScript?

Arrays in JavaScript are a way to store elements in a single variable in memory. By its index, the elements can be accessed in the array. The array index starts with 0. Javascript arrays are zero-based, meaning the first element index is 0. This is very important because it means that an offset of one unit will always exist: the first element will have an index of 0, and the second element will have an index of 1, and so on.

How to create arrays in JavaScript (example)?

The object Array allows you to store various values in one variable. It stores a sequential fixed-size collection of the same type of elements. An array is used to store a collection set, but it is often more helpful to consider an array as a set of the same type of variables. The literal notation array makes it simple to create arrays in JavaScript. It comprises of two square brackets that wrap optional, comma-separated array elements. Number, string, boolean, null, undefined, object, function, regular expression, and other structures can be any type of array elements.

The array can be created in JavaScript as given below

var country = ["India", "England", "Srilanka"];

In the above creation, you are initializing an array and it has been created with values “India”, “England”, and “Srilanka”. The index of “India”, “England”, and “Srilanka” is 0, 1 and 2 respectively. You can add extra elements to the country array, then you can do it as shown below

country[3] = "New Zealand";
country[4] = "Australia"
var country = new Array("India", "England", "Srilanka");
OR
var country = new Array();
country[0] = "India";
country[1] = "England";
country[2] = "Srilanka";

Example



JavaScript Creating Array





Output

Arrays in JavaScript

Types of arrays in JavaScript (Explain each type with example)

Below are the different types of arrays in javascript as follows:

  • Array Literal

The array can be created with an array literal by using below syntax

var arrayname=[value1,value2.....valuen];

Consider the simple example of creating an array in JavaScript by using Array Literal



Array Literal




Output:

arrays literal

  • Array Directly

The array can be created with array directly by using the new keyword as shown below in the syntax

var arrayname=new Array();

Below an example of creating an array in JavaScript by using Array Directly with the help of the new keyword



Array Directly




Output:

Arrays Directly

  • Array Constructor

The array instance can be created by passing arguments in a constructor. The following example creates an array in JavaScript by using Array Constructor with the help of the new keyword



Array Constructor




Output

arrays constructor

Explain array methods in JavaScript

Below are the array methods in JavaScript as follows

1. concat() method

You can create a new array with a combination of two or more arrays with the help of the concat() method.

Syntax

array.concat(val1, val2, ..., valN);

Example



JavaScript Array Method




Output

Arrays in JavaScript

2. every()

every() method checks whether given elements are true or false in an array as provided in the condition. It returns true if condition matches given condition otherwise returns false.

Syntax

Array.every(callback,thisArg)

Where callback is a condition to test the function and thisArg is an optional parameter used when executing the callback.

Example



JavaScript Array Method




Output

Arrays in JavaScript

3. fill()

The fill() method is used to fill the specified static values by modifying original values in the given array.

Syntax

array.fill(value)

Example



JavaScript Array Method




Output

Arrays in JavaScript

4. filter()

The filter() method is used to filter the array elements based on the given condition.

Syntax

array.filter(callback, thisArg)

Example



JavaScript Array Method




Output

Arrays filers

5. forEach()

The forEach() method is used to invoke the function for each array element.

Syntax

array.forEach(callback, thisArg)

Example



JavaScript Array Method




Output

arrays for each

6. includes()

The includes() method is used to check whether the given element is included in the array or not. If the element is included in an array, then it returns true otherwise returns false.

Syntax 

array.includes(element) //where “element” is a value which is to be searched in the array

Example



JavaScript Array Method




Output

arrays includes

7. join()

The join() method is used to combine the array elements into the string and provides a new string.

Syntax

array.join(separator)

Example



JavaScript Array Method




Output

join

8. pop()

The pop() method is used to extract the last element of the given array.

Syntax

array.pop(operator)

Example



JavaScript Array Method




Output

pop

9. push()

The push() method is used to add the element to the end of the array.

Syntax

array.push()

Example



JavaScript Array Method




Output

push

10. reverse()

The reverse() method is used to reverse the sequence of the given array of elements. The last element will become the first and the first element will become last.

Syntax

array.reverse()

Example



JavaScript Array Method




Output

Reverse

11. shift()

The shift() method is used to return the first element of an array.

Syntax

array.shift()

Example



JavaScript Array Method




Output

 Shift

12. slice()

The slice() method is used to display part of the given array elements without changing the original array.

Syntax

array.slice(start,end)

Example



JavaScript Array Method




Output

 Slice

13. sort()

The sort() method is used to display the array elements in ascending order.

Syntax

array.sort()

Example



JavaScript Array Method




Output

 sort

14. unshift()

The unshift() method is used to add elements at the beginning of the array.

Syntax

array.unshift(“element”)

Example



JavaScript Array Method




Output

 unshift

15. indexOf()

The indexOf() method is used to search the position of an element in a given array.

Syntax

array.indexOf(“element”)

Example



JavaScript Array Method




Output

arrays in JavaScript

Conclusion

As discussed until now, an array is a special kind of object for storing and managing data elements. An array is an object, that provides a group of elements and useful for storing a large amount of data of the same type. The properties and methods of array objects help developers to handle arrays easily. As a global object, the JavaScript Array can be used in the construction of arrays that are high-level.

Recommended Article

This has been a guide to Arrays in JavaScript. Here we discuss the different types and methods of arrays along with Example. You can also go through our other suggested articles to learn more

  1. JavaScript Tools
  2. JavaScript Math Functions
  3. Best Javascript Frameworks
  4. Uses of JavaScript

The post Arrays in JavaScript appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Arrays in JavaScript

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×