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.
7 Respostas
+ 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!
+ 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!
+ 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.
+ 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.
+ 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
+ 1
Thanks to you guys response, I don't have to search for hours on YouTube
:p
0
No prob :p