Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Swapping Variables

// program to swap variables

let x = 4;
let y = 7;

// swapping variables
[x, y] = [y, x];

console.log(x); // 7
console.log(y); // 4
Comment

swap in javascript

var first = 5;
var second = 7;
[first, second] = [second, first];
console.log(first, second);
//answer - 7 5
Comment

swap function javascript

function swap(x, y) {
    var t = x;
    x = y;
    y = t;
    return [x, y];
}

console.log(swap(2, 3));
Comment

javascript swap variables

var a = 5;
var b = 3;
[a, b] = [b, a];
Comment

Swapping variables JS

let a = 1;
let b = 3;

[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

const arr = [1,2,3];
[arr[2], arr[1]] = [arr[1], arr[2]];
console.log(arr); // [1,3,2]
Comment

swap function javascript

let a = 1;
let b = 2;
let temp;

temp = a;a = b;b = temp;
a; // => 2
b; // => 1
Comment

javascript swap

var first = 5;
var second = 7;
[first, second] = [second, first]
console.log(first, second)
//Output: 7,5
Comment

swap function javascript

let a = 1;
let b = 2;

a = a ^ b;b = a ^ b;a = a ^ b;
a; // => 2
b; // => 1
Comment

swap function javascript

let a = 1;
let b = 2;

a = a + b;b = a - b;a = a - b;
a; // => 2
b; // => 1
Comment

swap function javascript

let a = 1;
let b = 2;

[a, b] = [b, a];
a; // => 2
b; // => 1
Comment

swap function javascript

function swap(x, y) {
    return [y, x];
}

console.log(swap(2, 3));
Comment

Javascript swap

var first = 5;
var second = 7;

var third = first;
var first = second;

console.log(first)
console.log(third)
//Output: 7,5
Comment

PREVIOUS NEXT
Code Example
Javascript :: global axios vue 2 
Javascript :: validar array vacio javascript 
Javascript :: dynamically add/remove rows in html table using javascript 
Javascript :: react fragment inside map with key prop 
Javascript :: how to insert with variables from js to mysql 
Javascript :: axios all methods 
Javascript :: check browser 
Javascript :: import in react js 
Javascript :: convert string to lowercase javascript 
Javascript :: serve static files from express 
Javascript :: js scroll to bottom exact 
Javascript :: get 2nd td of tr 
Javascript :: javascript list to object map 
Javascript :: react router v6 lazy suspense 
Javascript :: date filter 
Javascript :: new line with javascript write 
Javascript :: js sort by two numeric fields 
Javascript :: how to print a array js 
Javascript :: javascript + sync for loop 
Javascript :: loop inside react js 
Javascript :: collapse in angular 4 
Javascript :: jest testing with ResizeObserver 
Javascript :: what is react 
Javascript :: splice from array javascript to remove 
Javascript :: super class js 
Javascript :: find remainder in javascript 
Javascript :: shorthand if statement js 
Javascript :: this is javascript 
Javascript :: react icon import 
Javascript :: https express 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =