Adding elements to array by keys JavaScript
Elements in an array don't need to be added immediately at the time of declaring this array. You can first declare this array, and then add the necessary elements to it, like this:
javascriptlet arr = [];
arr[0] = 'a';
arr[1] = 'b';
arr[2] = 'c';
console.log(arr); // shows ['a', 'b', 'c']
You can also add elements to an array already filled with data:
javascriptlet arr = ['a', 'b', 'c'];
arr[3] = 'd';
console.log(arr); // shows ['a', 'b', 'c', 'd']
Задание 1
Using the above technique,
create an array with the
elements 1, 2 and 3.
Задание 2
Let such an array be given:
javascriptlet arr = [1, 2, 3];
Add the elements 4
and 5 to its end.