'swapping array elements without a temporary variable
The following code:
var words = ['javascript', 'hmtl', 'css', 'python'];
words[0] = words[3];
words[3] = words[0];
alert(words);
...gives the below:
python,hmtl,css,python
- Why receive I
python,hmtl,css,python
, instead ofpython,hmtl,css,javascript
, if there is no temporary variable?
Would you Guys be able to help with the above, please?
Solution 1:[1]
if you want to swap without a temp variable use the ES6 array destruction syntax; simply do the following [words[0],words[3]]=[words[3],words[0]]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SandStone |