What will the console output be for console.log(arr.filter(x => x % 2)) with arr being [1, 4, 9, 16]?

Prepare for the Salesforce JavaScript Developer I Certification Exam. Utilize interactive quizzes, flashcards, and detailed explanations for each question. Boost your confidence and ace your exam effortlessly!

The expression arr.filter(x => x % 2) is used to filter the elements of the array based on whether they are odd or not. When you pass a function to the filter method, it creates a new array with all elements that pass the test implemented by the provided function.

In this case, the function x => x % 2 checks each element x of the array arr. The modulus operator % returns the remainder of the division of x by 2. If x is odd, x % 2 will return 1 (which is a truthy value), so that element will be included in the new array. If x is even, x % 2 will return 0 (which is a falsy value), so that element will be excluded from the new array.

Given the array [1, 4, 9, 16]:

  • 1 is odd, so it passes the filter.

  • 4 is even, so it does not pass the filter.

  • 9 is odd, so it passes the filter.

  • 16 is even, so it does not pass the filter.

Thus,

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy