js Array

A JavaScript array is an object that represents a collection of similar types of elements. Arrays are used to store multiple values in a single variable, making it easier to manage and manipulate collections of data.

There are 3 ways to construct array in JavaScript

1. By array literal

2. By creating instance of Array directly (using new keyword)

3. By using an Array constructor (using new keyword)

Creating Arrays in JavaScript

1. By Array Literal

The simplest and most common way to create an array is by using an array literal. An array literal is a comma-separated list of values enclosed in square brackets [].

Example:

<script>
var fruits = ["Apple", "Banana", "Cherry"];
document.write("Fruits: " + fruits + "
");
document.write("First fruit: " + fruits[0] + "
");
document.write("Number of fruits: " + fruits.length + "
");
</script>

Output:

Fruits: Apple,Banana,Cherry
First fruit: Apple
Number of fruits: 3

2. By Creating an Instance of Array Directly

You can also create an array by creating an instance of the Array object using the new keyword. This method is less common but still useful.

Example:

<script>
var vegetables = new Array("Carrot", "Broccoli", "Spinach");
document.write("Vegetables: " + vegetables + "
");
document.write("Second vegetable: " + vegetables[1] + "
");
document.write("Number of vegetables: " + vegetables.length + "
");
</script>

Output:

Vegetables: Carrot,Broccoli,Spinach
Second vegetable: Broccoli
Number of vegetables: 3

3. By Using an Array Constructor

You can use the Array constructor to create an array. This method allows you to specify the initial length of the array or to provide a list of elements.

Example:

<script>
var emptyArray = new Array(5);
var cars = new Array("Toyota", "Honda", "Ford");
document.write("Empty array length: " + emptyArray.length + "
");
document.write("Cars: " + cars + "
");
document.write("Third car: " + cars[2] + "
");
</script>

Output:

Empty array length: 5
Cars: Toyota,Honda,Ford
Third car: Ford

JavaScript Array Methods

Array Manipulation Methods:

Method Description
concat() Returns a new array by merging two or more arrays.
copyWithin() Copies part of an array to another location within the same array.
fill() Fills all the elements of an array with a static value.
pop() Removes and returns the last element of an array.
push() Adds one or more elements to the end of an array.
reverse() Reverses the order of elements in an array.
shift() Removes and returns the first element of an array.
unshift() Adds one or more elements to the beginning of an array.
splice() Adds/removes elements from an array.
slice() Returns a shallow copy of a portion of an array.

Array Iteration Methods:

Method Description
forEach() Executes a provided function once for each array element.
map() Creates a new array with the results of calling a function for every array element.
filter() Creates a new array with all elements that pass the test implemented by a function.
reduce() Applies a function against an accumulator and each array element to reduce it to a single value.
reduceRight() Applies a function against an accumulator and each array element (from right to left) to reduce it to a single value.
every() Checks if all array elements pass a test implemented by a function.
some() Checks if at least one array element passes a test implemented by a function.
find() Returns the value of the first array element that passes a test.
findIndex() Returns the index of the first array element that passes a test.
includes() Checks if an array contains a specified element.

Array Accessor Methods:


Method Description
entries() Returns a new Array Iterator object that contains key/value pairs for each index in the array.
keys() Returns a new Array Iterator object that contains the keys for each index in the array.
values() Returns a new Array Iterator object that contains the values for each index in the array.