zamknij
Powrót Strona Główna

Jesteśmy tu dla Ciebie

W GMI wierzymy, że nasi klienci to więcej niż partnerzy. Inwestujemy czas, aby zrozumieć Twój biznes, użytkowników i potrzeby, wspólnie kształtując sukces.

Ilona Budzbon Sales & Marketing

Jak mogę Ci pomóc?

Formularz Kontaktowy

GMI Software zobowiązuje się do przetwarzania przekazanych informacji w celu nawiązania kontaktu z Tobą w sprawie Twojego projektu. Dodatkowe dane są wykorzystywane w celach analitycznych. Od czasu do czasu możemy chcieć poinformować Cię o innych naszych usługach i treściach, które mogą być dla Ciebie interesujące. Jeśli wyrażasz zgodę na kontakt w tych celach, zaznacz poniższe pole wyboru. Możesz zrezygnować z naszych komunikatów w dowolnym momencie. Aby dowiedzieć się więcej o procesie rezygnacji i naszym podejściu do ochrony prywatności, zapoznaj się z naszą Polityką Prywatności.
To pole jest używane do walidacji i powinno pozostać niezmienione.

Optional Chaining & Nullish Coalescing

We call this operator using? On the object to get the value of some key on it. If the object and its key exist, the value we want to read will be returned, but if the object does not exist, the undefined value will be returned without throwing a reference error, so the code will not be interrupted.

Kamil Dziuba
CEO @ GMI Software
05 maja 2020 2 MIN CZYTANIA

The two functionalities of Javascript, which I describe in today’s post, are amazing development enhancers. They offer increased readability and significant simplification of the code in situations when we work with nested data.

Optional Chaining

We call this operator using? On the object to get the value of some key on it. If the object and its key exist, the value we want to read will be returned, but if the object does not exist, the undefined value will be returned without throwing a reference error, so the code will not be interrupted.

It is fantastic quality improvement, especially for nested data, so you do not have to write additional conditions, see for yourself in the example below.

Nullish Coalescing

This is an operator whose calling is similar to the logical operator OR or ||, we call it with? in conditions.

Fulfilling this condition requires passing null or undefined to the left side of it. It’s beneficial in many cases, so we can often get rid of a lot of the code easily while increasing overall readability. It is most useful when you want to use the value 0 in the condition so that it is not the default false.

Availability and implementation

There are several possibilities to use the mentioned functionalities. They are currently being deployed to browsers and are also not natively available for Node.js.

For production use, it’s best to use Babel presets or use Typescript. The differences can be seen in the bundle size, which in the case of Babel is smaller.

Additional sources:

@babel/plugin-proposal-optional-chaining

@babel/plugin-proposal-nullish-coalescing-operator