When using forEach with an array, what console output will be produced by the logArrayElements function defined as console.log('a[' + index + '] = ' + element)?

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 logArrayElements function makes use of the forEach method to iterate through each element of an array. Within the forEach method, the console.log statement is structured to output the index and the element in a specific format: 'a[' + index + '] = ' + element.

When this function is called on an array such as [2, 5, 9], for each element in the array, the forEach function will:

  1. Pass the current element to the logArrayElements function.

  2. Provide the corresponding index of that element.

Thus, when the first element (which is 2) is processed, it will output "a[0] = 2". For the second element (5), it will output "a[1] = 5", and for the third element (9), it will produce "a[2] = 9".

Therefore, the cumulative result printed in the console will be:

  • a[0] = 2

  • a[1] = 5

  • a[2] = 9

Hence, this results in the final output format as stated in option A: a[0] = 2, a[1] = 5, a

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy