Saturday, April 20, 2024
HomeSoftware DevelopmentJavaScript: Reverse Arrays

JavaScript: Reverse Arrays

Manipulating knowledge is core to any programming language. JavaScript isn’t any exception, particularly as JSON has token over as a first-rate knowledge supply format. One such knowledge manipulation is reversing arrays. You could need to reverse an array to indicate most up-to-date transactions, or easy alphabetic sorting.

Reversing arrays with JavaScript initially was performed by way of reverse however that may mutate the unique array:

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

// Reverse it with out reassigning:

// 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();

Today 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 information objects is extremely necessary in a programming language like JavaScript the place object references are significant.

  • Regular Expressions for the Rest of Us

    Eventually you will run throughout an everyday expression. With their cryptic syntax, complicated documentation and big studying curve, most builders accept copying and pasting them from StackOverflow and hoping they work. However what should you may decode common expressions and harness their energy? In…

  • Interview with a Pornhub Web Developer
  • Create Digg URLs Using PHP

    Digg not too long ago got here out with a candy new function that permits customers to create Tiny Digg URLs which present a Digg banner on the high permitting quick access to vote for the article from the web page. Whereas I really like visiting Digg each as soon as in a…

  • CSS Text Overlap

    One of many necessary features of CSS is to place parts. Margin, padding, high, left, proper, backside, place, and z-index are just some of the most important gamers in CSS positioning. Through the use of the above spacing…



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments