# JavaScript Array Techniques and Methods

• Post author:

In this tutorial, we will learn how to manipulate arrays in JavaScript with this comprehensive guide. You will learn techniques for removing elements, calculating averages, finding max values, and more. So let’s understand with Questions and their answers with explanation

Question – 1: How can you remove the last element from an array in JavaScript?

Ans: To remove the last element from an array in JavaScript, you can use the `pop()` method. This method removes the last element from an array and returns that element.

``````
let arr = [1, 2, 3, 4, 5];

let last = arr.pop();

console.log(arr); // [1, 2, 3, 4]

console.log(last); // 5
``````

Question – 2: Write a function that takes an array of numbers as an argument and returns the average of all the numbers in the array.

The following function takes an array of numbers as an argument and returns the average of all the numbers in the array. It uses the `reduce()` method to sum up all the numbers in the array, and then divides that sum by the length of the array to find the average.

``````
function average(numbers) {
let sum = numbers.reduce((a, b) => a + b, 0);
return sum / numbers.length;
}

console.log(average([1, 2, 3, 4, 5])); // 3
``````

Question -3: How can you find the maximum value in an array of numbers using JavaScript?

To find the maximum value in an array of numbers using JavaScript, you can use the `Math.max()` function in conjunction with the `apply()` method. The `apply()` method allows you to call a function with a given `this` value and arguments provided as an array.

``````
let numbers = [1, 2, 3, 4, 5];

let max = Math.max.apply(null, numbers);

console.log(max); // 5
``````

Question -4: How can you find the maximum value in an array of numbers using JavaScript?

The following function takes an array of strings as an argument and returns the longest string in the array. It uses the `sort()` method to sort the array of strings by their length, in descending order. Then it return the first element from array after sorting

``````
function longestString(strings) {
strings.sort((a, b) => b.length - a.length);
return strings;
}

console.log(longestString(["abc", "defg", "hijkl", "mnopqr"])); // "mnopqr"
``````

Question -5: How can you find the maximum value in an array of numbers using JavaScript?

To create a new array that contains a subset of elements from an existing array in JavaScript, you can use the `slice()` method. This method takes two arguments, a start index and an end index, and returns a new array that contains all the elements from the start index to the end index (end not included).

``````
let arr = [1, 2, 3, 4, 5];

let subArr = arr.slice(1, 3);

console.log(subArr); // [2, 3]
``````

Question -6: Write a function that takes an array of objects as an argument and returns the number of objects in the array that have a specific property.

The following function takes an array of objects as an argument and returns the number of objects in the array that have a specific property. It uses the `filter()` method to create a new array of objects that have the desired property, then it return the length of that array

``````
function countObjectsWithProperty(objects, property) {
let filteredObjects = objects.filter(object => object[property] !== undefined);
return filteredObjects.length;
}

console.log(countObjectsWithProperty([{ a: 1 }, { a: 2 }, { b: 3 }], "a")); // 2
``````

Question -7: How can you add an element to the beginning of an array in JavaScript?

To add an element to the beginning of an array in JavaScript, you can use the `unshift()` method. This method takes one or more elements as arguments and adds them to the beginning of the array. It also returns the new length of the array.

``````
let arr = [1, 2, 3, 4, 5];

arr.unshift(0);

console.log(arr); // [0, 1, 2, 3, 4, 5]
``````

Question -8: Write a function that takes an array of numbers as an argument and returns a new array with all the odd numbers filtered out

The following function takes an array of numbers as an argument and returns a new array with all the odd numbers filtered out. It uses the `filter()` method to create a new array that contains only the even numbers.

``````
function evenNumbers(numbers) {
return numbers.filter(num => num % 2 === 0);
}

console.log(evenNumbers([1, 2, 3, 4, 5, 6, 7, 8, 9])); // [2, 4, 6, 8]
``````

Question -9: How can you reverse the order of elements in an array in JavaScript?

To reverse the order of elements in an array in JavaScript, you can use the `reverse()` method. This method modifies the original array and reverses the order of its elements.

``````
let arr = [1, 2, 3, 4, 5];

arr.reverse();

console.log(arr); // [5, 4, 3, 2, 1]
``````

Question -10: Write a function that takes an array of numbers as an argument and returns the sum of all the numbers in the array.

The following function takes an array of numbers as an argument and returns the sum of all the numbers in the array. It uses the `reduce()` method to sum up all the numbers in the array

``````
function sum(numbers) {
return numbers.reduce((a, b) => a + b, 0);
}

console.log(sum([1, 2, 3, 4, 5])); // 15
``````

### Publisher

Publisher @ideasorblogs