Javascript array is an object which stores multiple items of different data types in a single variable. An arrayโs length always starts with index 0 and ends with total length minus 1. As an array stores multiple items soo we access items by index number.
Table Of Content:
Array Structure:
Letโs say we own various mobile phones from different companies, such as iPhone, Samsung, Redmi, Oppo, Infinix, and Vivo. Without an array, we can store them one by one in this way
let mobile1 = "iphone";
let mobile2 = "Samsung";
let mobile3 = "Redmi";
let mobile4 = "Oppo";
let mobile5 = "Infinix";
let mobile6 = "Vivo";
Example:
- But in an array, we can store them in a single variable name mobile in the following way. Always declare an array with const.
const mobiles=["iphone","Samsung","Redmi","Oppo","Infinix","Vivo"]
console.log(mobiles)
- Line and space in an array do not matter so for better understanding we can write an array like this
const mobiles=
["iphone",
"Samsung",
"Redmi",
"Oppo",
"Infinix",
"Vivo"]
console.log(mobiles)
- Another way to store in an array is first to make an empty array and then store valuesย
const mobiles=[];
mobiles[0] = "iphone";
mobiles[1] = "Samsung";
mobiles[2] = "Redmi";
mobiles[3] = "Oppo";
mobiles[4] = "Infinix";
mobiles[5] = "Vivo";
console.log(mobiles)
OUTPUT:
iphone,Samsung,Redmi,Oppo,Infinix,Vivo
Javascript Array Length:
Javascript array length starts with 0 index number and ends with total length minus 1. You can also access the array item with the index number. 0 index is the first item and 1 is the second item.
Here is an example of the index number
const mobiles=["iphone","Samsung","Redmi","Oppo","Infinix","Vivo"]
mobile= mobiles[0]
console.log(mobile)
mobile= mobiles[1]
console.log(mobile)
mobile= mobiles[4]
console.log(mobile)
mobile= mobiles[5]
console.log(mobile
OUTPUT:
iphone
Samsung
Infinix
Vivo
let mobile= mobiles[mobiles.length-1]
console.log(mobile)
OUTPUT:
Vivo
Javascript Array to String:
Javascriptย array can be converted into a string by using toString() method
Here is an example:
const mobiles=["iphone","Samsung","Redmi","Oppo","Infinix","Vivo"]
console.log(mobiles.toString())
Different between Array and Object:
An array is also an object but an Array use number to access its elements while an object use name to access its elements.
Example of array:
const mobiles=["iphone","Samsung","Redmi","Oppo","Infinix","Vivo"]
console.log(mobiles[1])
Example of Object:
const Class = {firstName:"Rashid", lastName:"Iqbal", age:25};
console.log(Class.firstName);
Adding New Array Elements:
We use the push() method to add a new element to an array. It also increases the length of the existing array. New elements are added to the end of an array, here is an example of adding a new element
const persons=["Ahmad","Rashid","Muna"]
persons.push("Waqas");
console.log(persons)
OUTPUT:
Ahmad,Rashid,Muna,Waqas
Suppose that our array length is 4 and we want to add a new array element at index 7 so index no.5 and 6 remain undefined. It creates a hole in an array.
Here is an example:
const persons=["Ahmad","Rashid","Muna","Waqas"]
persons[7] ="Irfan";
console.log(persons)
OUTPUT:
Ahmad,Rashid,Muna,Waqas,undefined,undefined,Irfan
Javascript Array Instanceof:
The instanceof operator returns true and false values, if the array then true otherwise it returns false.
const persons=["Ahmad","Rashid","Muna","Waqas"]
persons intanceof Array;
Javascript Array sort:
Array sort is used to arrange the elements alphabetically. For this purpose, we use 2 methods one is sort() and the other is reverse()
Here is an example of sort()
const mobiles=["iphone","Samsung","Redmi","Oppo","Infinix","Vivo"]
mobiles.sort()
OUTPUT:
iphone,Infinix,Oppo,Redmi,Samsung,Vivo
Here is an example of reverse()
const mobiles=["iphone","Samsung","Redmi","Oppo","Infinix","Vivo"]
mobiles.sort()
mobiles.reverse()
OUTPUT:
Vivo,Samung,Redmi,Oppo,Infinix,iphone
After leaning javascript the best language is react js
Javascript Array map:
Javascript map() performs a function and returns a new array. It takes three arguments which are
- ย ย ย ย Item value
- ย ย ย ย Item index
- ย ย ย ย Array itself
Here is an example:
const persons=["Ahmad","Rashid","Muna","Waqas"]
persons.map(myfunction)
function myFunction(value, index, array) {
return value;
}
While the callback function uses only value and returns the same value as an array
Here is an example:
const persons=["Ahmad","Rashid","Muna","Waqas"]
persons.map(myfunction)
function myFunction(value) {
return value;
}
OUTPUT:
Ahmad,Rashid,Muna,Waqas
How do I know if a variable is an array?
Use typeof operator in javascript to know the array and it always returns an object because the javascript array is an object. Now a day we use the Array.isArray() to solve the problem of an object and it returns an array.
3 thoughts on โWhat Is An Javascript Array?โ