Spread operator for strings in JavaScript
The spread operator applied to a string splits that string character by character:
javascript...'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:
javascriptfunction 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:
javascriptlet arr = [...'abcde']; // gets an array ['a', 'b', 'c', 'd', 'e']
Задание 1
Without running the code, determine what will be output to the console:
javascriptlet arr = [...'12345'];
console.log(arr);
Задание 2
Without running the code, determine what will be output to the console:
javascriptlet arr = ['a', ...'12345'];
console.log(arr);
Задание 3
Without running the code, determine what will be output to the console:
javascriptlet arr = [...'12345', ...'56789'];
console.log(arr);
Задание 4
Without running the code, determine what will be output to the console:
javascriptlet arr1 = ['a', 'b', 'c'];
let arr2 = [...arr1, ...'12345'];
console.log(arr2);