Created
January 20, 2021 18:17
-
-
Save Shariar-Hasan/a43dfa41fbe530231d73211f0af9371d to your computer and use it in GitHub Desktop.
Swapping two number in javascript (3 methods)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// how to swap two variable | |
//*************** * way -1**************** | |
var a = 5; | |
var b = 7; | |
console.log("brfore swap a = ",a,"and b =",b); | |
var temp = a; | |
a = b; | |
b = temp; | |
console.log("after swap a = ",a,"and b =",b); | |
//************ * way - 2 ***************** | |
var x = 5; | |
var y = 7; | |
console.log("brfore swap x = ",x,"and y =",y); | |
x = x+y; | |
y = x-y; | |
x = x-y; | |
console.log("after swap x = ",x,"and y =",y); | |
// ***********way -3*************** | |
// using function facilities | |
var p = 5 ; | |
var q = 7; | |
console.log("brfore swap p = ",p,"and q =",q); | |
[p,q] = [q,p]; | |
console.log("after swap p = ",p,"and q =",q); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment