Suppose that we have an array
numbers which contains 5 Numbers .
const numbers = [1,2,3,4,5]
For example we want to swap two elements , the first at index 3, in our case number
4 with the element of index 0 .
we can use a temporary item
temporary to store the Value of index 3 , then we put the number of index 0 in the place of 3 and we assign the temporary item to the one with index 3
const temporary = number; number = number; number = temporary;
another option without declaring a temporary Variable is to use the syntax:
const numbers = [1, 2, 3, 4, 5]; [numbers, numbers] = [numbers, numbers];
You like our Content? Follow us to stay up-to-date.