Strings and arrays are both iterables (well, strings are primitives of course, but the String objects they automatically get boxed into are iterables), so you can spread a string into an array literal, just like you can spread an array into an array literal.
You can only spread an iterable into an array literal, but you can spread any object, including a string (thanks to autoboxing) or an array, into an object literal. Would you often want to spread a string into an object literal in real code? I can't think of a reason why. But you can.
View all comments
Show parent comment
1
u/great_site_not Mar 10 '23
Strings and arrays are both iterables (well, strings are primitives of course, but the String objects they automatically get boxed into are iterables), so you can spread a string into an array literal, just like you can spread an array into an array literal.
You can only spread an iterable into an array literal, but you can spread any object, including a string (thanks to autoboxing) or an array, into an object literal. Would you often want to spread a string into an object literal in real code? I can't think of a reason why. But you can.