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 does the spread operator return when applied to a string, e.g., [...'Lydia']?

  1. ["L", "y", "d", "i", "a"]

  2. ["Lydia"]

  3. [[], "Lydia"]

  4. [["L", "y", "d", "i", "a"]]

The correct answer is: ["L", "y", "d", "i", "a"]

When the spread operator is applied to a string, it treats the string as an iterable sequence of its characters. In the case of the string 'Lydia', the spread operator separates each character and creates an array containing those individual characters. Therefore, using the expression `[...'Lydia']` results in an array that includes "L", "y", "d", "i", and "a" as its elements. This is consistent with how the spread operator works in JavaScript, where it can unpack elements from iterable structures like arrays and strings. In the scenario described, since 'Lydia' consists of five characters, the output of applying the spread operator will be an array with five distinct entries, each corresponding to a character in the string. Thus, the result is an array structured as ["L", "y", "d", "i", "a"].