JavaScript: Reverse Arrays


Manipulating information is core to any programming language. JavaScript is not any exception, particularly as JSON has token over as a first-rate information supply format. One such information 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 performed through reverse however that may 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 will 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 knowledge objects is extremely essential in a programming language like JavaScript the place object references are significant.

  • Conquering Impostor Syndrome

    Two years in the past I documented my struggles with Imposter Syndrome and the response was immense.  I obtained messages of help and commiseration from new net builders, veteran engineers, and even individuals of all expertise ranges in different professions.  I’ve even caught myself studying the publish…

  • CSS 3D Folding Animation
  • Retrieve Your Gmail Emails Using PHP and IMAP
  • Jack Rugile’s Favorite CodePen Demos

    CodePen is a tremendous supply of inspiration for code and design. I’m blown away daily by the demos customers create. As you may see beneath, I’ve an affinity towards issues that transfer. It was troublesome to slender down my favorites, however right here they’re!


Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here