The JavaScript Array is a global object used to store multiple values in a single variable.
Creating an array
There are several ways to create arrays:
Using array literal
Most programmers and I use this method to create an array.
Visual Representation
let players = ['Ronaldo', 'Messi', 'Mbappe'];
let price = [5, 10, 15];
console.log(players);
console.log(price);
output
[ 'Ronaldo', 'Messi', 'Mbappe' ]
[ 5, 10, 15 ]
Creating an array using new keyword
To create an array directly, use the new keyword.
Visual Representation
let price = new Array(5,10,15);
console.log(arr);
Output
[ 5, 10, 15 ]
Accessing array elements
You can access an element using square brackets.
An array index starts from 0.
Visual Representation
let price = [5, 10, 15, 20, 25];
console.log(price[0]);
console.log(price[1]);
console.log(price[3]);
Output
5
10
20
You can also use the forEach function to print the value individually.
Adding elements to an array
You can use either the push() or the unshift() method to add an element to an array.
Visual Representation
let price = [5, 10, 15, 20];
console.log('Before push: ', price);
price.push(25);
console.log('After push: ', price);
Output
Before push: [ 5, 10, 15, 20 ]
After push: [ 5, 10, 15, 20, 25 ]
Modifying an array
To change an array element, use the element index.
Visual Representation
let price = [5, 10, 15, 20, 25];
console.log('Before modify: ', price);
price[0] = 30;
price[1] = 40;
price[4] = 50
console.log('After modify: ', price);
Output
Before modify: [ 5, 10, 15, 20, 25 ]
After Modify: [ 30, 40, 15, 20, 50 ]
Remove an element from an array
You can use either the pop() or the shift() method to remove an element from an array.
Visual Representation
let price = [5, 10, 15, 20, 25];
console.log('Before remove: ', price);
let removedElement = price.pop();
console.log('Removed element: ', removedElement);
console.log('After remove: ', price);
Output
Before remove: [ 5, 10, 15, 20, 25 ]
Removed element: 25
After remove: [ 5, 10, 15, 20 ]
Array Length
Visual Representation
let price = [5, 10, 15, 20, 25];
console.log('Array length is: ', price.length);
Output
Array length is: 5
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 properties and methods to the object. |
Array Methods
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.