Javascript 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. An array is an object that can store multiple values at once.
Creating JavaScript arrays
You can create an array using two ways:
1) Using an array literal
The easiest way to create an array is by using an array literal [ ].
const array1 = ["pizza", "burger"];
2) Using the new keyword
we can also create an array using JavaScript's new keyword.
const array1 = new Array("pizza", "burger");
Thus, we can create an array using these both 2 ways. Let’s see more array examples.
// empty array
const myList = [ ];
// array of numbers
const num = [ 1, 3, 5, 7];
// array of strings
const str = [ 'pizza', 'burger', 'frenchfrice'];
// array with mixed data types
const mixed = ['work', 'exercise', 1, true];
console.log(myList); // Output : [ ]
console.log(num); // Output : [ 1, 3, 5, 7 ]
console.log(str); // Output : [ 'pizza', 'burger', 'frenchfrice' ]
console.log(mixed); // Output : [ 'work', 'exercise', 1, true ]
Access Elements of an Array
we can access elements of an array using indices (0, 1, 2 …).
const myArray = ['j', 'o', 'h', 'n'];
// first element
console.log(myArray[0]); // Output : "h"
// second element
console.log(myArray[1]); // Output :"e”
Change the Elements of an Array
we can also add elements or change the elements by accessing the index value.
let person = [ 'john', 'carter'];
// this will add the new element ‘person' at the 2 index
person[2] = 'randy';
console.log(person); // Output : [ 'john', 'carter', 'randy' ]
Array length
You can find the length of an element (the number of elements in an array) using the length property.
const person = [ 'john', 'carter','randy'];
// this gives the total number of elements in an array
console.log(person.length); // Output : 2
Array Methods
In JavaScript, there are various array methods available that make it easier to perform useful calculations.
Method | Description |
concat() | joins two or more arrays and returns a result |
indexOf() | searches an element of an array and returns its position |
forEach() | calls a function for each element |
push() | aads a new element to the end of an array and returns the new length of an array |
pop() | removes the last element of an array and returns the removed element |
sort() | sorts the elements alphabetically in strings and in ascending order |
slice() | selects the part of an array and returns the new array |
splice() | removes or replaces existing elements and/or adds new elements |
Array Method examples
let person = ['John', 'Randy', 'Carter']
let newperson = ['Cena'];
// sorting elements in the alphabetical order
person.sort();
console.log(person); // Output : [ 'Carter', 'John', 'Randy' ]
//finding the index position of string
const position = person.indexOf('Randy');
console.log(position); // Output : 2
// slicing the array elements
const newDailyActivities = person.slice(1);
console.log(newDailyActivities); // Output : [ 'John', 'Randy' ]
// concatenating two arrays
const concat = person.concat(newperson);
console.log(concat); // Output :[ 'Carter', 'John', 'Randy', 'Cena' ]