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.


What is the output when logging a[b] after a and b are modified?

  1. 123

  2. 456

  3. undefined

  4. ReferenceError

The correct answer is: 456

When logging `a[b]` after the modifications to `a` and `b`, seeing the output as 456 indicates that `b` was set to a valid key within the `a` object, and that this key corresponds to the value 456. In JavaScript, objects are collections of key-value pairs, and the value can be accessed using either dot notation or bracket notation. If `b` holds the string value that matches a key in the object `a`, then `a[b]` will yield the corresponding value. Therefore, if `a` is modified such that a property with `b` as the key exists and has a value of 456, logging `a[b]` will result in this output. This signifies that both `a` and `b` were appropriately set up prior to the logging statement, where the modification consisted of adding a property to `a` with the key specified by `b`, and assigning a value of 456 to it. Hence, when `a[b]` is evaluated, it retrieves that value.