Arrays in JavaScript
In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index. An array is an object that can store multiple values at once. For example, const words = ["India", "Sweden", "Norway", "Iceland"]; Here, words is an array.
An array is a collection of similar types of data. For example, if we want to store the names of 50 people then we can create an array of the string type that can store 50 names. String[] array = new String[50];
Lets start how's the array work
How to Create an Array
The basic way to start with an array is by using an array bracket []. For example,
const array = ["Rajasthan", "Jaipur"];
We can create array with different way using new keyword.
const arrayTwo = new Array("Doha", "Qatar");
So enjoy we created two arrays in a minutes
most probably we are using first one [.., ..] to create an array .
Lets understand more about array
// Numbers in array
let numb = [1, 2, 3, 4, 5, 6, 7, 8];
// String in array
let string = ["India", "Sweden", "Norway", "Iceland"];
// Empty array
let empty =[ ];
// Mixed array
let mix = ["India", 2, "Rajasthan", "Iceland" false];
JavaScript Array Elements
To access an array element, you will need to use the bracket to target the index of the array element you want.
Array index starts at 0, zero .
let numb = [1, "Iceland", 3, "India", 5, 6, 7, 8];
In this array targeting the first item of the array, which is the 1.
Array Length
Below is easier using the JavaScript array method called length, which returns a value of the array's length.
const country = [ "Doha", "Qatar", India];
// If the array has three items, the length array will return that value.
console.log(lengthofvalue.length); // 3
Add and remove element in last
// Adding element in last
let country = ["Iceland", "India"];
country.push("Doha");
console.log(country); // ["Iceland", "India", "Doha"];
// Remove element at last
let country = ["Iceland", "India", "Doha"];
country.pop();
console.log(country); // ["Iceland", "India" ]
// remove the last element from ["Iceland", "India"]
const removedElement = country.pop();
Add the element at the first and Change the Elements
// Add element in first
let country = ["India" , "Doha"];
country. unshift("Iceland");
console.log(country); // ["Iceland", "India", "Doha"]; Add the element at the first
let country = ["Iceland", "India", "Doha"];
country[1]= ("Sweden"); // [ "Iceland", "Sweden", "Doha" ] element "Sweden" at the 1 index
console.log(country);
Slice and Splice
Splice and Slice both are Javascript Array functions. Splice vs Slice. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.
const cities = ["Jaipur","mumbai","bengaluru","chennai","delhi"];
const newCityArr = cities.slice(2);
console.log("Original: ", cities)
console.log("New: ", newCityArr)
Add two or more array
The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.
let editor = ["Photoshop", "Illustrator"]; let editor1 = ["xd", "Premiere", "indesign"]; let editor2 = ["Acrobat", "Express"];
let creativeCloud = editor.concat(editor1, editor2);
console.log(creativeCloud);
How to use the indexOf() method
We will learn how to use the indexOf() method to find the index of an element/substring in an array/string;
var animals = ['Lion', 'Monkey', 'Rhino', 'Cat'];
console.log(animals.indexOf('Rhino'));
Subscribe to my newsletter
Read articles from Rajnish sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by