Monday, June 23, 2025
HomeJavaScriptJavaScript: Reverse Arrays

JavaScript: Reverse Arrays


Manipulating knowledge is core to any programming language. JavaScript is not any exception, particularly as JSON has token over as a main knowledge supply format. One such knowledge manipulation is reversing arrays. Chances are you’ll need to reverse an array to point out most up-to-date transactions, or easy alphabetic sorting.

Reversing arrays with JavaScript initially was executed through reverse however that will mutate the unique array:

// First worth:
const arr = ['hi', 'low', 'ahhh'];

// Reverse it with out reassigning:
arr.reverse();

// Worth:
arr (3) ['ahhh', 'low', 'hi']

Modifying the unique array is a legacy methodology. To keep away from this mutation, we might copy the array after which reverse it:

const reversed = [...arr].reverse();

As of late we are able to use toReversed to keep away from mutating the unique array:

const arr = ['hi', 'low', 'ahhh'];
const reversed = arr.toReversed(); // (3) ['ahhh', 'low', 'hi'];
arr; // ['hi', 'low', 'ahhh']

Avoiding mutation of information objects is extremely vital in a programming language like JavaScript the place object references are significant.

  • 9 Mind-Blowing Canvas Demos

    The <canvas> factor has been a revelation for the visible consultants amongst our ranks.  Canvas supplies the means for unbelievable and environment friendly animations with the added bonus of no Flash; these builders can flash their superior JavaScript expertise as an alternative.  Listed here are 9 unbelievable canvas demos that…

  • CSS Animations Between Media Queries

    CSS animations are proper up there with sliced bread. CSS animations are environment friendly as a result of they are often {hardware} accelerated, they require no JavaScript overhead, and they’re composed of little or no CSS code. Very often we add CSS transforms to components through CSS throughout…

  • Using TogetherJS
  • CSS Custom Cursors

    Keep in mind the Net 1.0 days the place you needed to customise your web site in each method attainable?  You abused the scrollbars in Web Explorer, in fact, however the most well-liked exterior service I can keep in mind was CometCursor.  CometCursor allow you to create and use a great deal of customized cursors for…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments