tf-array-methods
True or False: The map() method in JavaScript modifies the original array. True or False: The filter() method can be used on an array in TypeScript to return a subset of the array based on a condition. True or False: The reduce() method in JavaScript is used only to sum up values in an array.
The map() method in JavaScript does not modify the original array; instead, it creates a new array with the results of calling a provided function on every element in the calling array. So this statement is False.
The filter() method in TypeScript (and JavaScript) does indeed return a new array with all elements that pass the test implemented by the provided function. So this statement is True.
The reduce() method in JavaScript is not limited to summing up values; it executes a reducer function on each element of the array, resulting in a single output value. The reducer function is not limited to addition—it can perform any operation that combines two values into one. So this statement is False.
Code Interviewer Q&A expert Certainly, here are three more true/false questions regarding array methods in JavaScript and TypeScript:
True or False: The forEach() method returns a new array based on the function passed to it. True or False: The some() method will return true as soon as any array element satisfies the provided testing function. True or False: The findIndex() method returns the found element itself, not its index in the array.
The forEach() method does not return a new array; it simply calls a provided function on each element in the array. So this statement is False.
The some() method does indeed return true as soon as any array element satisfies the testing function, otherwise, it returns false. So this statement is True.
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function, not the element itself. If no elements satisfy the testing function, it returns -1. So this statement is False.
True or False: The concat() method changes the original array by adding new elements to it. True or False: The every() method tests whether all elements in the array pass the test implemented by the provided function. True or False: The splice() method is used to change the contents of an array by removing or replacing existing elements and/or adding new elements in place.
The concat() method does not change the original arrays. It creates a new array by merging (concatenating) existing arrays. So this statement is False.
The every() method does test whether all elements in the array pass the test implemented by the provided function. If any element fails the test, the method returns false. So this statement is True.
The splice() method can indeed be used to change the contents of an array by removing, replacing, and adding elements. So this statement is True.
True or False: The slice() method can be used to remove elements from an array destructively. True or False: The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. True or False: The push() method adds one or more elements to the end of an array and returns the new length of the array.
The slice() method is non-destructive; it returns a shallow copy of a portion of an array into a new array object and does not modify the original array. Hence, the statement is False.
The includes() method does determine whether an array includes a certain value, returning true if it does find it, otherwise false. So this statement is True.
The push() method adds one or more elements to the end of an array and returns the new length of the array. This statement is True.