Javascript Module 5
Javascript Module 5
Array Operations/Methods:
1. Adding Elements:
push():Adds one or more elements to the end of an array and returns the new length of the array.
Code :
const arr = [1, 2, 3];
arr.push(4, 5);
console.
unshift(): Adds one or more elements to the beginning of an array and returns the new length of the
array.
Code :
const arr = [2, 3, 4];
arr.unshift(1);
console.log(arr); // Output: [1, 2, 3, 4]
2. Removing Elements:
pop(): Removes the last element from an array and returns that element.
Code :
const arr = [1, 2, 3, 4, 5];
const poppedElement = arr.pop();
console.log(poppedElement); // Output: 5
console.log(arr); // Output: [1, 2, 3, 4]
shift(): Removes the first element from an array and returns that element.
Code :
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
3. Accessing Elements:
Indexing: Accessing elements by their index.
Code :
const arr = [1, 2, 3];
arr.forEach(num => console.log(num * 2));
// Output:
// 2
// 4
// 6
4. Modifying Elements:
splice(): Removes elements from an array and optionally replaces them with new elements.
Code :
const arr = [1, 2, 3, 4, 5];
arr.splice(2, 1, 'a', 'b');
console.log(arr); // Output: [1, 2, 'a', 'b', 4, 5]
Code :
const arr = [1, 2, 3, 4, 5];
const slicedArr = arr.slice(2, 4);
console.log(slicedArr); // Output: [3, 4]
5. Searching:
indexOf(): Returns the first index at which a given element can be found in the array, or -1 if it is not
present.
Code :
const arr = [1, 2, 3, 4, 5];
const index = arr.indexOf(3);
console.log(index); // Output: 2
lastIndexOf(): Returns the last index at which a given element can be found in the array, or -1 if it is not
present.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
const arr = [1, 2, 3, 4, 2, 5];
const lastIndex = arr.lastIndexOf(2);
console.log(lastIndex); // Output: 4
6. Filtering:
filter(): : Creates a new array with all elements that pass the test implemented by the provided function.
Code :
const arr = [1, 2, 3, 4, 5];
const evenNumbers = arr.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
7. Mapping:
map(): Creates a new array populated with the results of calling a provided function on every element in
the calling array.
Code :
const arr = [1, 2, 3];
const squaredNumbers = arr.map(num => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9]
reduce() : Executes a reducer function on each element of the array, resulting in a single output value.
Code :
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 15
Code :
const arr = ['Hello', 'World!'];
const joinedString = arr.join(', ');
console.log(joinedString); // Output: 'Hello, World!'
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
String to Array:
split(): Splits a string into an array of substrings based on a specified separator.
Code :
const str = "Hello, World!";
const parts = str.split(", ");
console.log(parts); // Output: ['Hello', 'World!']
from(): Creates a new, shallow-copied Array instance from an array-like or iterable object.
Code :
Other Operations :
includes() : Determines whether an array includes a certain value among its entries.
Code :
const arr = [1, 2, 3, 4, 5];
const includesThree = arr.includes(3);
console.log(includesThree); // Output: true
3. Reverse an Array:
Code :
const arr = [1, 2, 3, 4, 5];
const reversedArr = arr.reverse();
console.log(reversedArr); // Output: [5, 4, 3, 2, 1]
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
6. Concatenating Arrays:
Code :
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const concatenatedArr = arr1.concat(arr2);
console.log(concatenatedArr); // Output: [1, 2, 3, 4, 5, 6]
7. Sorting Array:
Code :
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
3. DOM Manipulation:
In web development, arrays are often used to manage and manipulate DOM elements, such as lists of
items or form inputs.
4. API Responses:
Arrays are frequently used to handle API responses, where data is returned in the form of an array of
objects or values.
5. Dynamic UI Updates:
Arrays play a vital role in dynamically updating user interfaces, such as adding or removing elements
from a list without refreshing the entire page.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]