JavaScript Array - Javascript Objects

What is JavaScript Array?

In order to store more than one value in a single variable the Array objects are used. It can store contiguous elements of same data type. An array is a special variable which can take more than one value at a time.

Syntax:

Below is the syntax to create an Array object −

We can give list of integers or string in the array parameter. At the time of defining an array itself, we will give the size of the array.

You can create an array by simply assigning values as shown in the below example −

Note: Placing a comma after the last element in arraywill lead to inconsistency across browsers.

We can store values in to the array using index/odinal numbers. For example, if we initialized an array of size n then it will take values ranging from 0 to (n-1) .If the size is 10 then it will take values from 0 to 9.We will pass values in to array

as shown below:

fruits[0]is the first element

fruits[1]is the second element

fruits[2]is the third element

What are Array Properties?

Below is the list of the properties of the Array object along with their description.

Property

Description

constructor

It returns a reference to the array function that created the object.

index

This property represents the zero-based index of the match in the string

input

This property is only present in arrays created by regular expression matches.

length

Reflects the number of elements in an array.

prototype

The prototype property allows you to add properties and methods to an object.

In the upcoming sections, we will have a few examples which explain the use of Array properties.

What are JavaScript Array Methods?

Here is a list of the methods of the Array object along with their description.

Method

Description

concat()

This method returns a new array comprised of this array joined with other array(s) and/or value(s).

every()

This method returns true if every element in this array satisfies the provided testing function.

filter()

This method creates a new array with all of the elements of this array for which the provided filtering function returns true.

forEach()

This method calls a function for each element in the array.

indexOf()

This method returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

join()

This method joins all elements of an array into a string.

lastIndexof()

This method returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

map()

This method returns creates a new array with the results of calling a provided function on every element in this array.

pop()

This method returns removes the last element from an array and returns that element.

push()

This method addsone or more elements to the end of an array and returns the new length of the array.

reduce()

This method applies a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.

reduceRight()

This method appliesa function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

reverse()

This method reservesthe order of the elements of an array -- the first becomes the last, and the last becomes the first.

shift()

This method removes the first element from an array and returns that element.

slice()

This method extracts a section of an array and returns a new array.

some()

This method returnstrue if at least one element in this array satisfies the provided testing function.

toSource()

This method representsthe source code of an object

sort()

This method sortsthe elements of an array

splice()

This method addsand/or removes elements from an array.

toString()

This method returns a string representing the array and its elements.

unshift()

This method adds one or more elements to the front of an array and returns the new length of the array.

In the upcoming sections, we will have a few examples which illustrate the use of Array methods.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Javascript Objects Topics