How to swap the first and last value in Array in JavaScript

I was thinking about an exercise that could aid someone with their javascript learning, and this came to mind because it is important to note that not everyone has access to study and comprehend the fundamentals of javascript. So, in this piece, I will simplify and break down this challenge.

Problem

Swapping the first and final values in an array is the issue that we're trying to solve. We wish to swap the first and last elements' locations in an array of elements. For instance, the expected result would be [5, 2, 3, 4, 1] if the array was [1, 2, 3, 4, 5]. By figuring out this issue, we may efficiently change the array's element order to suit certain needs.

First things first, you have a choice between using a function or going directly to the action. However, I like to utilize functions since they can always be reused in code and they help us write cleaner code. Let me demonstrate how functions may be used.

Building Function

We declare a function called swap and use the parameter arrswap, and it looks like this:

function swap (arrswap) {}

Now that we've declared the function and assigned it a parameter, we'll write our block of code within the function that will be reusable whenever we want.

function swap (arrswap) {
 const lastIndex = arrswap.length - 1;
           const lastValue = arrswap[lastIndex];
        const firstValue = arrswap[0];

        arrswap[0] = lastValue;
        arrswap[lastIndex] = firstValue;

        return arrswap;
}

Here we declare a variable (lastIndex) to get the last element in the array. The index of the last element in the array is obtained by deducting 1 from the array's length. This is because array indices begin at 0, therefore the final element will be at the index array.length: 1.

To give an example, let's declare an array:

const array = [2, 4, 6, 7];

and now to get the last value in the array we can write

const lastvalue = array[array.length - 1];

to verify we console.log

console.log(lastvalue);
/*this value produces the number 7.  
 You can use the code to test it.*/

Getting the last element in the array was the first thing we accomplished, and the second step was to declare the last and first values. As you can see, that's what we did here.

const lastValue = arrswap[lastIndex];
        const firstValue = arrswap[0];

As previously said, when counting the elements in an array, the count always begins with zero, which is why we are assigning the value zero, because applying the value zero means it reads the first element in the array and assigns it that value. As the topic suggests, we should switch the first and last values. which is exactly what we did here:

 arrswap[0] = lastValue;
        arrswap[lastIndex] = firstValue;

The code above enables us to swap the values, but take note of one difference: the swap occurs differently this time, with the values for both the first and last values positioned to the left. This is because we used const to declare both the first and last value variables, and const prevents us from changing values. Consequently, the swap must occur in this manner.

The code then proceeds to return the value of the parameter, "arrswap." As soon as we finish creating the code for our function, take note of one thing: the parameter returns an array, and I'll explain it shortly.

Using Functions to Modularize Code and Promote Reusability

Let's declare an array, fill it with elements, and then call our function to put it to use.

const array = [7, 6, 8, 9, 10]
const secondArray = ['hi', 'my', 'name', 'is','enoch']

console.log(swap(array));
console.log(swap(secondArray));

As was said previously, we declared two arrays, the first of which was named array and the second named secondArray. Once their respective elements have been assigned to both, we run our function to switch the first and last values. via the use of console.log. and the result is seen below.

Conclusion

At last, a quick and effective method for switching the first and last values in an array using JavaScript may be found here.

The first and final values of an array can be switched, which has several advantages. According to your particular needs, you can change the order of the elements or rearrange the data. In situations like sorting algorithms or data pretreatment chores, this can be quite helpful.

You can improve your knowledge of JavaScript programming and broaden your capacity for problem-solving by comprehending the underlying concepts behind swapping values in an array. To work with data structures and optimize your code, you need to be able to modify arrays using swapping techniques.

Writing effective and beautiful code as a JavaScript developer is made possible by understanding techniques like swapping values in an array. Utilize these abilities to provide creative solutions for your forthcoming projects by continuing to learn and increasing your knowledge.