PHP 8 features I wish also existed in JavaScript
Tags: php, javascript, webdev, programming
Published: 2021-02-08 12:34:25

I know there is still a lot of hatred for PHP out there, but in my opinion with version 7 at the latest (which is already over 5 years old!), PHP evolved to a great language that is fun and even type-safe to use. Next to [Just-In-Time compilation](https://php.watch/articles/jit-in-depth), which may give a big performance boost to PHP applications, version 8 brings [a lot of useful features](https://www.php.net/releases/8.0/en.php).

I want to present three of them I really wish I could use in JavaScript as well. I hope that comes in handy especially for those, who didn't take a look at PHP 8 yet. Let's go!


## #1 Named arguments

Let's assume, you have a function `foo` with 6 parameters `a` to `f` having different default values and now you want to call that function passing the argument `false` for the last parameter only.

PHP 8:

```php
foo(f: false);
//-----^
```

JavaScript:

```javascript
foo('test', 42, true, 'bar', 5.5, false);
//--------------------------------^
```

In JavaScript passing arguments to a function is solely based on the parameter position, unfortunately. I know that named arguments can be simulated by using objects and destructuring, but a native feature would be much more convenient here.

See [RFC: Named Arguments](https://wiki.php.net/rfc/named_params)


## #2 Match expression

The new `match` expression of PHP is very similar to the `switch` statement, except it is an expression and can be used to directly assign values to a variable or return values. This comes in very handy, if you have more than two possible values for assignment.

PHP 8:

```php
$fontWeight = match ($weight) {
  100, 200      => "Super Thin",
  300           => "Thin",
  400, 500      => "Normal",
  600, 700, 800 => "Bold",
  900           => "Heavy",
  default       => "Not valid"
};
```

JavaScript:

```javascript
let fontWeight = '';
switch (weight) {
  case 100:
  case 200:
    fontWeight = "Super Thin";
    break;
  case 300:
    fontWeight = "Thin";
    break;
  case 400:
  case 500:
    fontWeight = "Normal";
    break;
  case 600:
  case 700:
  case 800:
    fontWeight = "Bold";
    break;
  case 900:
    fontWeight = "Heavy";
    break;
  default:
    fontWeight = "Not valid";
    break;
}
```

The `match` expression may be simulated by a JavaScript object, but that would require an additional variable (thus more reserved memory) and will fail for cases, where the checked values aren't allowed to be used as object keys.

See [Docs: `match`](https://www.php.net/manual/en/control-structures.match.php)


## #3 Optional Chaining

Chaining only if the needed property exists is a very elegant way to query objects. In PHP you can do this now with the nullsafe operator.

PHP 8:

```php
$country = $session?->user?->getAddress()?->country;
```

JavaScript:

```javascript
const country =
  session && session.user && session.user.getAddress() && session.user.getAddress()['country']
  ? session.user.getAddress()['country']
  : null;
```

In JavaScript you still have to check step by step, if the corresponding property exists. There is a corresponding [TC39 proposal](https://github.com/tc39/proposal-optional-chaining) which is in stage 4 by now and I'm really looking forward to this being part of the ECMAScript Specs.

See [RFC: Nullsafe Operator](https://wiki.php.net/rfc/nullsafe_operator)


## Wrap it up

So we saw a small selection of the new PHP 8 features that would also be a great addition to JavaScript. Maybe you have other PHP features in mind that are missing in JavaScript or you know a better JavaScript counterpart to the above problems than me? Great! Let's discuss it in the comments.

---

*Edited: 10th of February 2021 (extended `match` example)
Published: 8th of February 2021*