[Javascript] Swapping the values of two variables

발행일: Feb, 2025
조회수: 0
단어수: 37

Table of Contents

1. Traditional Method

Declare a temporary variable, copy the value, and then swap.

let a = 10;
let b = 20;
const temp = a;
a = b;
b = temp;

2. Modern Method

By using arrays and destructuring assignment, swapping can be done in a single line.

[b, a] = [a, b];

태그#Variable Swapping#JavaScript#Traditional Method#Modern Method

JUNA BLOG VISITORS

오늘0
Total0