Don’t use functions as callbacks unless they’re designed for it

Solid advice by Jake:

Here’s an old pattern that seems to be making a comeback:

import { toReadableNumber } from 'some-library';
const readableNumbers = someNumbers.map(toReadableNumber);

Here’s the problem:

// We think of:
const readableNumbers = someNumbers.map(toReadableNumber);
// …as being like:
const readableNumbers = someNumbers.map((n) => toReadableNumber(n));
// …but it's more like:
const readableNumbers = someNumbers.map((item, index, arr) =>
  toReadableNumber(item, index, arr),
);

You might have encountered this yourself when combining map with parseInt, as its second parameter defines the base to use 😅

Don’t use functions as callbacks unless they’re designed for it →

map is one of the array methods I use a lot, along with filter and reduce. This post right here explains them using a simple example.

Destructuring arrays in PHP: Practical examples

Being more focussed on JavaScript nowadays, I kinda forgot that it’s possible to destructure arrays in PHP ever since the release of PHP 7.1. Frank de Jonge provides us with some practical examples such as this simple one:

// JavaScript
let options = {enabled: true, compression: 'gzip'};  
let { enabled, compression } = options;

console.log(enabled);
console.log(compression);
// PHP 7.1+
$options = ['enabled' => true, 'compression' => 'gzip'];
['enabled' => $enabled, 'compression' => $compression] = $options;

var_dump($enabled);
var_dump($compression);

Freek Van der Herten adds some extra examples, showing its use in for loops:

$members = [
    [1, 'Seb'],
    [2, 'Alex'],
    [3, 'Brent'],
];

foreach ($members as [$id, $name]) {
   // do stuff with $id and $name
}

Array destructuring in PHP →
Array destructuring in PHP (Additions by Freek) →

V8 internals for JavaScript developers: Arrays

This presentation demonstrates how learning just a little bit about JavaScript engine internals can help you improve the run-time performance of your JavaScript code — not just in V8 specifically, but across all JavaScript engines!

A written version is also available.