Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JavaScript Program to Swap Two Numbers

Updated on Nov 22, 2024
By Mari Selvan
πŸ‘οΈ 212 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
JavaScript Program to Swap Two Numbers

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the world of programming, performing basic operations on numbers is a fundamental task. Swapping two numbers is one such operation that involves exchanging the values of two variables. This operation is commonly used in various algorithms and programs.

In this tutorial, we will explore a simple JavaScript program designed to swap the values of two numbers.

πŸ“„ Example

Let's delve into the JavaScript code that accomplishes this task.

swapNumbers.js
Copied
Copy To Clipboard
// Function to swap two numbers
function swapNumbers(a, b) {
  // Destructuring assignment to swap values without a temporary variable
  [a, b] = [b, a];
  return [a, b];
}

// Driver program
// Replace these values with the numbers you want to swap
let num1 = 5,
  num2 = 10;

console.log(`Before swapping: num1 = ${num1}, num2 = ${num2}`);

// Call the function to swap the numbers
[num1, num2] = swapNumbers(num1, num2);

console.log(`After swapping: num1 = ${num1}, num2 = ${num2}`);

πŸ’» Testing the Program

To test the program with different numbers, modify the values of num1 and num2 in the main program.

Output
Before swapping: num1 = 5, num2 = 10
After swapping: num1 = 10, num2 = 5

🧠 How the Program Works

  1. The program defines a function swapNumbers that takes two parameters, a and b, and uses destructuring assignment to swap their values without a temporary variable.
  2. Inside the main program, replace the values of num1 and num2 with the numbers you want to swap.
  3. The program prints the values of num1 and num2 before and after swapping.

🧐 Understanding the Concept of Swapping Two Numbers

Swapping two numbers involves exchanging their values. In the provided program, a temporary variable is used to hold the value of one variable while the values of the two variables are exchanged.

🎒 Optimizing the Program

While the provided program is a straightforward way to swap two numbers, consider exploring and implementing alternative approaches or optimizations.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (JavaScript Program to Swap Two Numbers), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy