๐Ÿ˜œ

์ญˆ๋‚˜์•„๋น  ๋ธ”๋กœ๊ทธ

JUNA
STUDIO

[Javascript] Swapping the values of two variables

๋ฐœํ–‰์ผ: Feb, 2025
์กฐํšŒ์ˆ˜: 2
๋‹จ์–ด์ˆ˜: 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];

Tags: #Variable Swapping#JavaScript#Traditional Method#Modern Method
JUNA BLOG VISITORS
Today
7
 (
updown
-7
)
Total
657
 (
updown
+7
)

ยฉ 2025 juniyunapapa@gmail.com.