Q19.
Explain reduce(), map(), forEach() and filter() methods in JavaScript:
1. forEach(): Executes a function on each array element. It does not return a new array.
Example:
[Link](num => [Link](num)); // logs each number
2. map(): Transforms each element and returns a new array.
Example:
let doubled = [Link](num => num * 2); // [2, 4, 6, 8]
3. filter(): Returns a new array of only elements that pass a test.
Example:
let even = [Link](num => num % 2 === 0); // [2, 4]
4. reduce(): Reduces the array to a single value.
Example:
let sum = [Link]((acc, val) => acc + val, 0); // 10
Q20. Explain unshift(), splice(), slice(), push() and pop() methods in JavaScript:
1. unshift(): Adds one or more elements to the beginning of an array.
Example: [Link](0);
2. push(): Adds one or more elements to the end of an array.
Example: [Link](5);
3. pop(): Removes the last element of an array.
Example: [Link]();
4. splice(): Adds/removes elements from any position in the array.
Example: [Link](2, 1); // removes 1 item at index 2
5. slice(): Extracts a portion of array into a new array.
Example: [Link](1, 3); // gets elements from index 1 to 2