< javascript complete notes Exception handling in javascript >
An array in JavaScript is a data structure used to store multiple values in a single variable. It can hold various data types and allows for dynamic resizing. Elements are accessed by their index, starting from 0.
let x = [10,20,30,40,50];
console.log(x);
console.log(x[0]);
console.log(x[3]);
console.log(x[1]);
let y = Array(22,33,44,55,66)
console.log(y[2])
x[6]= 60;
console.log(x);
x[5]= 66;
console.log(x);
x[5]= 606;
console.log(x);
for(z in x){
console.log(z)
}
for(k of x){
console.log(k)
}
Array predefined function
JavaScript array methods are built-in functions that allow efficient manipulation and traversal of arrays. They provide essential functionalities like adding, removing, and transforming elements, as well as searching, sorting, and iterating through array elements, enhancing code readability and productivity.
Array length
The length property returns the length of the given array.
Array toString()
The toString() method converts the given value into the string.
Array join()
This join() method creates and returns a new string by concatenating all elements of an array. It uses a specified separator between each element in the resulting string.
Array pop()
Array push()
Array shift()
Array unshift()
Array concat()
Array splice()
Array slice()
delete array[index]
The delete operator is used to delete the given value which can be an object, array, or anything.
let x = [10, 20, 30, 40, 50];
console.log(x);
console.log(x.length);
console.log(x.toString());
console.log(x.pop())
console.log(x);
console.log(x.push(60,70,80));
console.log(x);
console.log(x.shift());
console.log(x);
console.log(x.unshift(5,6,7));
console.log(x);
Slice,splice,join,concat
let x = [10, 20, 30, 40, 50];
console.log(x);
// index strats at 0;
// position strats at 1;
slice(Index,position)
console.log(x.slice(2, 3));
Splice(index,how many to delete,what to add))
console.log(x.splice(2, 1, 45, 65));
console.log(x);
//join(Seperaor)
console.log(x.join(" * "));
//concat creates a new array by merging (concatenating) existing arrays
let y = [11, 22, 33, 44, 55];
console.log(x.concat(y));
console.log(y.concat(x));
Search functions
let x = [10, 20, 30, 40, 50,40,60,40];
//indexOf searches an array for an element value and returns its position.
console.log(x.indexOf(40))
//To find out the last occurence of the element
console.log(x.lastIndexOf(40))
//IT checks wether the element is present in the array or not
console.log(x.includes(66))
myarray.find((value)=>{
if(value > 40){
console.log(value)
}
})
//50,40,60,40
console.log(x.findLast((value)=>{
return value>40;
}))
Sort functions
array.sort()
let books = ["math", "science", "social", "engljsh", "english","hindi"];
console.log(books);
console.log(books.sort());
console.log(books);
console.log(books.reverse());
console.log(books);
let myarray = [10,20,30,40,50,60,70,80,90,100,101];
console.log(myarray.sort((firstelement,secondelement)=>{
return (firstelement-secondelement)
}));
Array Iteration Functions
let myarray = [11, 22, 33, 44, 55, 66, 77, 88, 99];
var Total = 0;
myarray.forEach((value, index, array) => {
var Total = Total + value;
console.log(Total);
});
console.log(
myarray.map((value) => {
//return value*3;
}),
);
myarray.filter((value)=>{
if(value > 20){
console.log(value)
}
})
//22, 33, 44, 55, 66, 77, 88, 99
console.log(
myarray.reduce((Total, value, index, array) => {
return (Total = Total + value);
}),
);
let yourarray = [101, 203, 303, 404];
Multidimentionl Array
let combinedArray = [myarray, yourarray];
//console.log(combinedArray);
spread operator
let combinedSpreadArray = [...myarray, ...yourarray];
//To merge an array we use spread operator
//console.log(combinedSpreadArray);
insert and remove element without using any predefined method
var myarray = [10, 20, 30, 40, 50];
for (i = 0; i < myarray.length; i++) {
console.log(myarray[i]);
}
//How to add an element at the begening of an array
console.log(myarray);
myarray[5] = myarray[4];
myarray[4] = myarray[3];
myarray[3] = myarray[2];
myarray[2] = myarray[1];
myarray[1] = myarray[0];
myarray[0] = 44;
console.log(myarray);
//How to add an element at the end of an array
myarray[myarray.length] = 155;
//How to add an element at the specified position of an array
//30,55
//[10, 20, 30,40, 50 ]
//[10, 20, 30 ,40, 50, ]
< javascript complete notes Exception handling in javascript >