Spread operator for strings and numbers in JavaScript

The spread operator applied to a string splits that string character by character:

...'abcde'; // will split the string into characters separated by commas: //'a','b','c','d','e'

The result of such a split can be passed to the function parameters:

function func(s1, s2, s3, s4, s5) { return s1 + '-' + s2 + '-' + s3 + '-' + s4 + '-' + s5; } console.log( func(...'abcde') ); // shows 'a-b-c-d-e'

And you can convert this result to an array:

let arr = [...'abcde']; // gets an array ['a', 'b', 'c', 'd', 'e']

Without running the code, determine what will be output to the console:

let arr = [...'12345']; console.log(arr);

Without running the code, determine what will be output to the console:

let arr = ['a', ...'12345']; console.log(arr);

Without running the code, determine what will be output to the console:

let arr = [...'12345', ...'56789']; console.log(arr);

Without running the code, determine what will be output to the console:

let arr1 = ['a', 'b', 'c']; let arr2 = [...arr1, ...'12345']; console.log(arr2);

spread operator and numbers

The spread operator is not applicable to numbers:

let arr = [...12345]; // there will be an error

To solve the problem, you should convert the number to a string, like this:

let arr = [...String(12345)]; // gets an array ['1', '2', '3', '4', '5']
enru