Swapping Two Numbers in javascript | Swap Program in JavaScript

Swap two Number Program in JavaScript - In this program, we write how to swap two number using third variable in javascript. Swapping two numbers in javascript program to swap variable values without using a temporary variable. Swap program means the value of one variable is assigned to another and vice versa.


Swapping Two Numbers in javascript
Swap Program in JavaScript


Example of Swap Program in JavaScript:

<html>
<head>
<title> Swap Program in JavaScript</title>
</head>
<style>
body{

background-color:#f47a42;

color: white;

width:500px;

height:200px;

border:1px solid black;

position:absolute;

top:150px;

left:100px;

}
</style>
<h1><center>Simple Swap Number Program</center></h1>
<script type="text/javascript">
function swap(){
var a,b,c;
a=Number(document.getElementById("first").value);
b=Number(document.getElementById("second").value);
c=a;
a=b;
b=c;
document.getElementById("answer1").value= a;
document.getElementById("answer2").value= b;
}
</script>

Value of a: <input id="first">
Value of b: <input id="second"></br></br>
<button onclick="swap()">Swap</button></br></br>
Value of a: <input id="answer1">
Value of b: <input id="answer2">
</body>
</html>

Post a Comment

0 Comments