Data manipulation is at the core of any programming language. JavaScript is no exception, especially with JSON taking over as the primary data delivery format. One such data manipulation is array inversion. You can reverse the array to see the most recent transactions or simply sort alphabetically.
Inverting an array in JavaScript was originally done in the following way: reverse
But it changes the original array:
// First value: const arr = ['hi', 'low', 'ahhh']; // Reverse it without reassigning: arr.reverse(); // Value: arr (3) ['ahhh', 'low', 'hi']
Modifying the original array is the traditional method. To avoid this mutation, copy the array and then invert it.
const reversed = [...arr].reverse();
Recently it has become available toReversed
To avoid modifying the original array:
const arr = ['hi', 'low', 'ahhh']; const reversed = arr.toReversed(); // (3) ['ahhh', 'low', 'hi']; arr; // ['hi', 'low', 'ahhh']
Avoiding data object mutations is very important in programming languages like JavaScript, where object references are important.
CSS @Support
Feature detection in JavaScript is a client-side best practice and for good reason, but unfortunately the same functionality is not available within CSS. You end up repeating the same property multiple times for each browser prefix. Hi. One more thing, we…
JavaScript Promise API
Synchronous code is easier to track and debug, but asynchronous is generally better in terms of performance and flexibility. Why “put the show on hold” when you can trigger many requests at once and process each one when it’s ready? Promises are becoming a big part of the JavaScript world…
MooTools Instagram
If you still play around with your iPhone and enjoy taking photos from time to time, you’d be crazy not to use an app called Instagram. Instagram lets you take photos just like the native iPhone camera app, but Instagram…
JavaScript canvas image conversion
At last week’s Mozilla WebDev Offsite, we all spent half of the final day hacking future Mozilla Marketplace apps. One of the mobile apps that got a lot of attention recently was Instagram. Instagram was sold to Facebook for the ridiculous price of one.