Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


Given the code "let res = sumArr([2,3,4]);", which line correctly asserts that sumArr adds the numbers in the array?

  1. console.assert(res === 9);

  2. console.log(res === 9);

  3. console.assert(res != 9);

  4. console.error(res != 9);

The correct answer is: console.assert(res === 9);

The choice that correctly asserts that `sumArr` adds the numbers in the array is the option that uses the `console.assert` method with the condition that checks if `res` is equal to 9. The function `sumArr` is expected to take an array of numbers as input and return their sum. In this case, the array `[2, 3, 4]` sums up to 9. Using `console.assert(res === 9);` effectively checks whether the value of `res` is indeed equal to 9. If the assertion fails (meaning that `res` is not equal to 9), an error message will be displayed in the console, indicating that the assumption about the function's behavior is incorrect. This method is ideal for verifying that the actual output of a function matches the expected output during testing. The other options do not provide the same level of verification. Logging the value with `console.log(res === 9);` merely outputs `true` or `false` to the console without asserting anything. The operators in the last two options express conditions that contradict the goal of confirming that `sumArr` works correctly.