0

How to swap value in JavaScript

How do I swap value in JavaScript, is it the same as in python with just a = sign or do we still use the old way of temp value? Please help.

5th Nov 2018, 2:54 AM
Leonardo Monterey
Leonardo Monterey - avatar
7 Réponses
+ 3
We can't do it exactly like Python, but in ES6, this works: [a, b] = [b, a] However, since many devices don't support ES6, I'd recommend using a temp. Also, this question has been asked multiple times before. Check out https://www.sololearn.com/Discuss/1383009/?ref=app Please use the search bar before posting. Thanks!
5th Nov 2018, 3:13 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
You're welcome, Leonardo Monterey ! Yes, the search bar is not as good as Google. Sometimes if I don't use the right keywords, nothing useful shows up. But I'm sure the Sololearn team is working on improving it. And I'm sorry I assumed you didn't try searching!
5th Nov 2018, 3:55 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
if you’re swapping the values of two variables (x and y) then the general method is create a temp variable, set the value of the temp variable to the value of variable x, set the value of variable x to the value of variable y, set the value of variable y to the temp value.
5th Nov 2018, 3:13 AM
Panayiotis Spanos
Panayiotis Spanos - avatar
+ 1
Thanks for your response and for future responses. Kishalaya , I did search for the question it didn't come up, Mu guess is everyone words it differently.
5th Nov 2018, 3:46 AM
Leonardo Monterey
Leonardo Monterey - avatar
+ 1
I have seen both ways of swapping ES6 and JS wasn't sure which one was the most proper way guess it's the temp way would be the answer :p
5th Nov 2018, 3:49 AM
Leonardo Monterey
Leonardo Monterey - avatar
+ 1
Thanks to you guys response, I don't have to search for hours on YouTube :p
5th Nov 2018, 3:51 AM
Leonardo Monterey
Leonardo Monterey - avatar
0
No prob :p
5th Nov 2018, 4:01 AM
Leonardo Monterey
Leonardo Monterey - avatar