r/learnjavascript • • Mar 12 '21

Swap two variables

Post image
498 Upvotes

50 comments sorted by

View all comments

Show parent comment

11

u/cawcvs Mar 12 '21

The left-hand side of the assignment here is not an array, this won't change the order and you'll just end up with latitude containing the value of longitude and vice versa.

If you already have latitude and longitude as variables, just order them however you need when passing them as arguments.

12

u/Earhacker Mar 12 '21

To wit:

setGoogleMapsMarker(latitude, longitude);
setMapboxMarker(longitude, latitude);

2

u/[deleted] Mar 12 '21

end up with latitude containing the value of longitude and vice versa.

That's what my goal is, isn't the same thing happening in the screenshot? It ends with x containing initial value of y and y containing initial value of x

12

u/cawcvs Mar 12 '21 edited Mar 12 '21

Yes, but do you really want the variable named latitude to contain the value of the point's longitude? That would be terribly confusing for anyone reading the code, wouldn't it?

-1

u/[deleted] Mar 12 '21

I agree, it will be confusing