Answers for "Javascript swap old and new method"

2

swap in javascript

var first = 5;
var second = 7;
[first, second] = [second, first];
console.log(first, second);
//answer - 7 5
Posted by: Guest on August-07-2021
4

javascript swap variables

var a = 5;
var b = 3;
[a, b] = [b, a];
Posted by: Guest on May-25-2020
0

how do you swap the vaRIables js

let a = "red";
let b = "blue";
let c = a; // red
a = b; //over-rides to blue
b = c;

console.log(a);
console.log(b);
Posted by: Guest on February-26-2021
0

Javascript swap old and new method

//old method 
var a = 1;
var b = 2;
var temp = a;
a = b;
b = temp;
console.log(a, b)
//expected output: 2 1

//new method(using destructuring method for swap)
var x = 10;
var y = 20;
[y, x] = [x, y];
console.log(y, x);
//expected output: y = 10 and x = 20
Posted by: Guest on September-03-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language