The array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. So, let’s define arrays in Javascript.
JavaScript Array
JavaScript array is an object that represents the collection of similar types of elements. JavaScript arrays are used to save multiple values in a single variable. An array is a particular variable, which can hold more than one value at a time.
If you have a list of elements (a list of fruits, for example), storing the fruits in a single variable could look like the following code.
let x = "me"; let u = "sunflower"; let v = "smooth criminal";
However, what if you want to loop through the songs and find a specific one? And what if you had not 3 songs, but 300?
The solution is an array!
The array can hold many values under a single name, and you can access the values by referring to the index number.
How to Create an Array in JavaScript
To create an array in JavaScript, use an array literal or by creating the instance of an Array directly (using the new keyword) or by utilizing the Array constructor.
JavaScript array literal
The syntax of creating an array using array literal is given below.
var array = [1, 2, 3]; let array = [1, 2, 3]; const array = [1, 2, 3];
JavaScript array directly (new keyword)
To create an array directly, use the new keyword in JavaScript. The syntax of creating an array directly is given below.
Syntax
let arrayname = new Array();
Example
See the below example.
// app.js let arr = new Array(); arr.push(4, 9, 16) console.log(arr);
Output
In the above example, we have used the array push() function.
JavaScript array constructor (new keyword)
Here, you need to create an instance of an array by passing arguments in the constructor so that we don’t have to provide the value explicitly. An example of creating the object by array constructor is given below.
// app.js let arr = new Array(4, 9, 16); console.log(arr);
See the following output.
How to access array elements in JavaScript
To access the elements in the JavaScript array, use the array index. You can access an element by using square brackets and the array index starts from 0.
See the following code.
// app.js let arr = new Array(4, 9, 16); console.log(arr[0]); console.log(arr[1]); console.log(arr[2]);
See the output.
You can also use the forEach function to print the value one by one.
How to change Array element in JavaScript
To change an array element in JavaScript, use the element index. See the following code.
// app.js let arr = new Array(4, 9, 16); console.log('Before modify: ', arr); arr[0] = 25; arr[1] = 36; arr[2] = 49 console.log('After Modify: ', arr);
See the following output.
Arrays are Objects
Arrays are a special type of object. The typeof operator in JavaScript returns an “object” for arrays. But, JavaScript arrays are best described as arrays.
JavaScript variables can be objects. Arrays are special kinds of objects. Because of this, you can have the variables of different types in the same Array.
You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array.
Array Properties and Methods
JavaScript arrays have properties and methods. It is their real strength.
Let’s see one property to find a length of an array using the length property.
// app.js let arr = new Array(4, 9, 16); console.log('Array length is: ', arr.length);
See the output.
Array Properties
Here is the list of the properties of the Array object along with their description.
Sr.No. | Property & Description |
---|---|
1 | constructor
Returns the reference to the array function that created the object. |
2 | index
The property represents a zero-based index of the match in the string |
3 | input
This property is only present in the arrays created by regular expression matches. |
4 | length
Reflects the number of elements in the array. |
5 | prototype
The prototype property allows you to add the properties and methods to the object. |
Associative Array in Javascript
Javascript does not support associative arrays because JavaScript does not support arrays with named indexes. In JavaScript, arrays always use numbered indexes.
Many programming languages support arrays with named indexes. Arrays with named indexes are called associative arrays (or hashes).
When to Use Arrays. When to use Objects.
- JavaScript does not support associative arrays.
- You should use the objects when you want the element names to be strings (text).
- You should use the arrays when you want item names to be numbers.
JavaScript Array Methods
That’s it for this tutorial.