Array An array is an object in JavaScript, it enables us to store different elements under one variable name and it has different methods to perform common operations on arrays.
Characteristics Of Arrays (In the Context Of JavaScript)
In JavaScript, arrays are described as an object not primitive or any data types.Arrays are resizable and contain a mix of different data types. Array's element can only be accessed by non-negative integers as indexes of an array. Arrays in JavaScript are zero-indexed, which means the counting of the elements in arrays, starts from zero, not from one.
Syntax A pair of Square Brackets( [ ] ) is used to symbolize an array in JS. The elements are separated by commas(,) and there are no restrictions about data types, you can use different data types like boolean, string, number, object, etc. in a single array
const arrayName=["element1", "element2", "element3",......, "elementN"];
Array Methods
1. push()
The push() method is used to insert an element in an array, it adds the new element at the end of an array. This method doesn't create a new array, it just updates the existing(original) array.
EXAMPLE
const ourArray=["ram", "lucky", "veer","hanu"];
ourArray.push("vip");
console.log(ourArray);
// ["ram", "lucky", "veer", "hanu", "vip"](output)
2. unshift()
The unshift() method is used to insert an element in an array, from the beginning. This method doesn't create a new array, it just updates the existing(original) array.
EXAMPLE
const ourArray=["me", "you", "yours", "their"];
ourArray.unshift("here");
console.log(ourArray);
// [ "here", "me", "you", "yours", "their"] (output)
3. pop()
The pop() method is used to delete an element in an array, from the end. This method also updates the existing (original) array, not create another one.
EXAMPLE
const ourArray=["1", "2", "3", "4"];
ourArray.pop();
console.log(ourArray);
// ["1", "2", "3"] (output)
4. shift()
The shift() method deletes the first element of an array , it just updates the original array by deleting the first element.
EXAMPLE
const ourArray=["shikhar", "rohit", "virat", "rahul"];
ourArray.shift();
console.log(ourArray);
// ["rohit", "virat", "rahul"] (output)
5. slice()
The slice() method slices out the part of the array, always remember it does not change the original array. You can create a new array of the sliced part. slice(a,b) method has two parameters, a will define the starting point ( by default it is 0 ) and b will define the ending point and b is not included in a new array
const ourArray=["1", "2", "3", "4"];
const newArray=ourArray.slice(1,3);
console.log(newArray);
// ["2", "3"] (output)
//There is no change in ourArray
6. splice()
The splice() method is used to add/ delete the elements of an array. The splice() method overwrites the original array. splice(a,b, "add1", "add2"...), it basically has 2 parameters, a which defines the starting point, b defines how many elements are to be deleted and parameters after a, and b is elements to be added in original arrays.
EXAMPLE
const ourArray=["me", "you", "yours", "their"];
ourArray.splice(1,2,"to","from");
console.log(ourArray);
// ["me", "to", "from", "their"](output)
7. concat()
The concat() method merges one and more arrays and returns a merged array. It does not change the original or existing arrays.
EXAMPLE
const arr1=[1,2,3,4,5];
const arr2=[6,7,8,9];
const arr=arr1.concat( arr2);
console.log(arr3);
// [1, 2, 3, 4, 5, 6, 7, 8, 9](output)
8. toString()
The toString() method is used internally by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string. This method automatically covert arrays in comma-separated strings and then the console logs it.
EXAMPLE
const set=[1,2,3,4,5];
set.toString();
// '1,2,3,4,5' (output)
9. join()
The join() method also joins all array elements into the string but here we can specify the separator. The default separator is a comma(,).
EXAMPLE
const set=[1,2,3,4,5];
set.join("@");
// '1@2@3@4@5' (output)
10. includes()
The includes() method in an array determines the presence of the element, If the element is found, the method returns true, and false otherwise.
EXAMPLE
const numberSet=[ram,raju,hill];
numberSet.includes(hill);
// true (output)
11. filter()
The filter() method filters down the elements from the given array which satisfy the given filter condition.
EXAMPLE
const list=["abc" , "defg" , "hijkl", "mnopqr", "stuvwxy"];
const filterList = list.filter(word=> word.length>=5);
console.log(filterList);
// ['hijkl', 'mnopqr', 'stuvwxy'] (output)
12. map()
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
EXAMPLE
const num = [1, 2, 3, 4];
const numSqr=num.map(x => x**2 );
console.log(numSqr);
// [1, 4, 9, 16] (output)
13. sort()
The sort() method sorts the array's elements alphabetically.The default sorting order is ascending. This method overwrites the original array and changes it.
EXAMPLE
onst names = ['tom', 'alex', 'bob'];
names.sort(); // returns ["alex", "bob", "tom"]
14. reverse()
The reverse() method is used to reverse the order of elements in an array.
EXAMPLE
const alpha=['a','b','c','d','e'];
alpha.reverse();
// ['e', 'd', 'c', 'b', 'a'] (output)