JavaScript Array part 01
In this Lecture, we going to study the JavaScript Array
we going to cover the following topics in this lecture: -
what is array
push
pop
shift
unshift
slice
splice
concertation
fill
include
index of
isArray
join
lastIndexOf
map
reverse
sort
for of
What is Array: -
The array is a memory that store all element sequentially. It starts from 0 to its element length.
Let arr= [1,2,3,6,8]
Let is a data type
arr is a variable name (it can be any name you want)
[1,2, 3,6,8] it means there are 5 elements present in an array
Array lenght
let names=['ayear','babita ji','jetalal','abdul','sundar'];
//lenght of array***********
console.log(names.length);
Output: - 5
As we discuss Array start from the 0th position so how the output is 5 but in the array.length count starts from 1th position to array length.
Push: -
It is used to add an element in the array from the last position.
let names= ['ayear','babita ji','jetalal','abdul','sundar'];
//push*********
names.push('sodi_paji');
console.log(names);
Output: -
[ 'ayear', 'babita ji', 'jetalal', 'abdul', 'sundar', 'sodi_paji' ]
POP: -
let names= ['ayear','babita ji','jetalal','abdul','sundar'];
names.pop();
console.log(names);
Output: -
[ 'ayear', 'babita ji', 'jetalal', 'abdul' ]
Shift: -
The shift() Method is used to remove the first element from array.
const fruits=['Apple','banana','grapes'];
fruits.shift();
console.log(fruits);
ouutput:- ['banana','grapes']
Unshift: -
The unshift() Method is used to add a new element from the beginning of an array.
const fruits=['Apple','banana','grapes']
fruits.unshift['Mango','Patato'];
console.log(fruits)
output: -['Mango','Patato','Apple','banana','grapes']
Slice: -
It is used to remove the element from the array but requires its index value.
slice does not alter the original array
Slice(a,b)
a is an index of the array.
b is also an index but it excludes the b index element in the slice.
let names= ['ayear','babita ji','jetalal','abdul','sundar'];
console.log(names.slice(1,3));
console.log(names. Slice(0));
Output: -
[ 'babita ji', 'jetalal' ]
[ 'ayear', 'babita ji', 'jetalal', 'abdul', 'sundar' ]
Splice: -
The splice method is used to add or remove an element from an array.
But Splice do change in the orignal array
let fruit=['apple','bada apple','chota apple','double apple'];
fruit.splice(2,1,'kharab apple','acha aaple');
console.log(fruit);
fruit.splice(1,0,'add any item');
console.log(fruit);
let users=['TOM','TIM','TON','SAM','SOR','SOD'];
users.splice(1,3,'CHETAK');
console.log(users);
Output: -
[ 'apple', 'bada apple', 'kharab apple', 'acha aaple', 'double apple' ]
[
'apple',
'add any item',
'bada apple',
'kharab apple',
'acha aaple',
'double apple'
]
[ 'TOM', 'CHETAK', 'SOR', 'SOD' ]
concertation: -
It use to add the string as well as the array
let arr1=[1,2,3,4];
let arr2=[5,6,7,8];
const arr3=arr1.concat(arr2);
console.log(arr3);
let arr4=[11,12,13,14];
let arr5=[15,16,17,18];
let arr6=[19,20];
console.log(arr4.concat(arr5,arr6,arr2));
Output: -
[
1, 2, 3, 4,
5, 6, 7, 8
]
[
11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 5, 6,
7, 8
]
include: -
include () method is used to check whether the element is present in the array or not
include()method return true or false
include(element,index of element)
let arr1=[1,12,38,40];
console.log(arr1.include(12,2));
Output: -true
let arr=[1,2,3,4,5,6];
console.log(arr.includes(3,2)); output: - true
console.log(arr.includes(3,4)); output: - false
IndexOf(): -
It is used to find the index of array element
let arr=[1,2,3,4,5,6,'Ashu','abhay'];
console.log(arr.indexOf(3));
console.log(arr.indexOf('Ashu'));
Output: -
2
6
isArray: -
It is used to identify the weather the input is array or not return "true" and "false".
let arr=[1,2,3,4,5,6,'Ashu','abhay'];
console.log(Array.isArray(arr))
let check='it is array or sting';
console.log(Array.isArray(check));
console.log(typeof(check));
Output: - true
false
Join(): -
The join()
method returns an array as a string.
The join()
method does not change the original array.
Any separator can be specified. The default is comma (,).
let arr=[1,2,3,4,5,6,'Ashu','abhay'];
console.log(arr.join());
console.log(arr.join(' + '));
let check=arr.join(' * ');
console.log(check);
console.log(typeof(check));
Output: -
1,2,3,4,5,6,Ashu,abhay
1 + 2 + 3 + 4 + 5 + 6 + Ashu + abhay
1 * 2 * 3 * 4 * 5 * 6 * Ashu * Abhay
string
last Index of: -
It will return the last index of element from array
let arr=[1,2,3,'Ashu',5,'Ashu',6,'Ashu'];
console.log(arr.lastIndexOf('Ashu'));
Output: -
7
NOTE : - REST TOPICES WE GOING TO STUDY IN PART 02