• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Swap Values in an Array in JavaScript

How to Swap Values in an Array in JavaScript

May 18, 2022 Leave a Comment

We can swap values in an array in JavaScript easily by making use of a temporary variable. The easiest way to show how this is done is with an example.

var someArray = [value1,value2];
var temp = someArray[0];
someArray[0] = someArray[1];
someArray[1] = temp;

Lets show a really simple example using this code:

var someArray = [3,4];
var temp = someArray[0];
someArray[0] = someArray[1];
someArray[1] = temp;

console.log(someArray);

#Output:
[4,3]

When working with variables in JavaScript, being able to change the values of variables easily is important.

One such change is swapping the values between two variables.

We can easily swap values in an Array using JavaScript. To swap values you can use a temporary variable.

Below is our code again of how to swap the values in an Array using JavaScript.

var someArray = [value1,value2];
var temp = someArray[0];
someArray[0] = someArray[1];
someArray[1] = temp;

Swap Any Values in an Array in JavaScript

In this example, we will swap any two values in an array. Here is our function that will let you swap any two values of an array. We simply need to give the function the array, the first position of the element to be swapped, and the second position of the element we want to swap it with.

function swapValues(arr,position1,position2){
  var temp = arr[position1];
  arr[position1] = someArray[position2];
  arr[position2] = temp;
};

Let’s see a simple example with this code, swapping the element in the first spot of the array, with the element in the second spot of the array:

function swapValues(arr,position1,position2){
  var temp = arr[position1];
  arr[position1] = someArray[position2];
  arr[position2] = temp;
};

var someArray = ["red","orange","yellow","green","blue","indigo","violet"];
swapValues(someArray,0,1);
console.log(someArray);

#Output:
['orange', 'red', 'yellow', 'green', 'blue', 'indigo', 'violet']

And one more example, swapping the element in the fourth spot of the array, with the element in the seventh spot of the array.

function swapValues(arr,position1,position2){
  var temp = arr[position1];
  arr[position1] = someArray[position2];
  arr[position2] = temp;
};

var someArray = ["red","orange","yellow","green","blue","indigo","violet"];
swapValues(someArray,3,6);
console.log(someArray);

#Output:
['red', 'orange', 'yellow', 'violet', 'blue', 'indigo', 'green']

Hopefully this article has been helpful for you to learn how to swap values in an array with JavaScript.

Other Articles You'll Also Like:

  • 1.  Get the Size of a Set in JavaScript
  • 2.  Changing the Background Image of a div in JavaScript
  • 3.  JavaScript Check if Attribute Exists in HTML Element
  • 4.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 5.  JavaScript Change Border Color of Element
  • 6.  Using JavaScript to get Textarea Value
  • 7.  Using JavaScript to Capitalize the First Letter of a String
  • 8.  Using JavaScript to Get New Date Format in dd mm yy
  • 9.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 10.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy