Array and Array Methods in JavaScript.
What are Arrays and its inbuild methods of array in JavaScript.
What is an Array in JavaScript?
An array is where you can store the collection of multiple items under/in one variable and has members for performing common array operations.
Why do we use Arrays?
They are useful to store the value of different data types may it be boolean, object, string, number, symbol etc.
An array is an ordered list of values that you refer to with a name and an index.
Arrays are reliable it provides various multiple inbuilt methods that help perform various operations on the values which are stored in an array.
Declaration of Array
Array declaration can be done in the following 2 ways.
// Way's to decleare Arrays
const studentRecord = ["tom", { rollNumber: 23 }, true, 8];
const recordStudent = new Array("tom", { rollNumber: 23 }, true, 8);
console.log("Method 1" + studentRecord + "Metrhod 2" + recordStudent);
Access the elements from the array
const studentRecord = ["tom", { rollNumber: 23 }, true, 8];
console.log( studentRecord[2]);
Find the length of an array
// Find the legth of an array
const studentRecord = ["tom", { rollNumber: 23 }, true, 8];
console.log(studentRecord.length);
Array Methods in JavaScript
1. Join() Method
Creates and returns the new string by concatenating the elements present in the array, separated by the commas or by the separated strings.
Below is an example of the same.
const temp_1 = ["Address", "Name", "PhoneNo"];
const concat_string = temp_1.join(["MH", "Moreshwar", 9365655000]);
console.log(concat_string);
2. π slice() Method
The
slice()
the method returns the portion of an array into a new array object selected from the start to π.However, here the end is not included.
π represents the index of the item in that array.
-ve index count from the end π of the array.
// slice() method
slice()
slice(start)
slice(start, end)
***************************** SYNTAX *****************************
const books = ["Zero to One" "1984","Dark Matters","Animal Farm","Sapiens"];
console.log(books.slice(2, 5)); // [ Dark Matters, Animal Farm, Sapiens]
console.log(books.slice(-2)); // [ Animal Farm, Sapiens]
console.log(books.slice(3, -2)); // []
console.log(books.slice(2, -1)); // [Dark Matters, Animal Farm]
3. indexOf() Method
- Returns the first index at which a given element can be found in the array, or it will return -1 if the element is not present in the array.
indexOf(searchElement)
indexOf(searchElement, fromIndex)
***************************** SYNTAX *****************************
const employeeData = ["tom", "john", "peter", "steven"];
console.log(employeeData.indexOf("tom")); // will return 0
console.log(employeeData.indexOf("steven", 2));
// This will start search from 2nd index in array employeeData and return 3
console.log(employeeData.indexOf("peter", 3));
// This will start search from 3rd index in array employeeData
To Add The Elements In The Array
4. push() Method
The
push()
the method adds the element at the end of an array and returns the new array with π new length.The
push()
method appends values to an array.The
push()
is mutating method elements are added at the end π.
push(element0)
push(element0, element1)
push(element0, element1, /* β¦ ,*/ elementN)
- Example
// push() method
console.log("push() Method");
const bookStack = [ "One Of Us Is Lying", "Hooked", "Meditations",
"THE SILENT PATIENT",];
console.log(bookStack ' ' +bookStack.length);
// One Of Us Is Lying,Hooked,Meditations,THE SILENT PATIENT 4
bookStack.push("When Breath Becomes Air", "1984");
console.log(bookStack + ' ' +bookStack.length);
// ['One Of Us Is Lying', 'Hooked', 'Meditations', 'THE SILENT PATIENT', 'When Breath Becomes Air', '1984'] 6
Merging 2
const test_π_1 = ["a","b","c"];
const test_π_2 = ["e", "f", "g"];
console.log(test_π_1.push(test_π_2));
// ["a","b","c","e", "f", "g"];
5. Splice() Method
- This method will help you to remove the targeted element from an array collection and add/replace it.
// splice() Method
// This method is been used for changing the content of an array by removing or replacing existing elements or adding new elements to an existing array without modifying them
bookStack.splice(3, 4, "Dune");
console.log(bookStack);
bookStack.splice(8, 9, "Upgrade5");
console.log(bookStack);
bookStack.splice(4, 5, "Hooked");
console.log(bookStack);