What falls under ECMAScript

What will come with ECMAScript 2020

In the summer, seven features will be officially released as part of ECMAScript 2020. There is also an extension of the specification for the for-in loop. The new functions include shortened notations, a new data type and the ability to increase the performance of websites. This article introduces the new features.

Optional chaining operator

The new optional chaining operator introduces a shortened notation that makes it easier to check for undefined values. Especially when working with data from interfaces, it can never be assumed that all data is actually included. It often happens that a data record is only partially filled or not at all. To ensure that the software does not crash, for example when performing a function on an undefined data record, it must first be checked whether it has been properly filled. The usual notation for this is the ternary operator:

const tomatoes = recipes.pasta
? recipes.pasta.tomatoes
: undefined;

If is defined, save the number of tomatoes, otherwise return. This notation can quickly take up a lot of space, for example if several levels of the object are undefined at the same time. In this case there must be a separate ternary operator for each of the layers.

An alternative notation is the classic test with an if construction, in which the functionality is only executed when the value is filled. Although the spelling also leads to the desired result, it also takes up a lot of space. The new optional chaining offers a defined, shortened notation:

const tomatoes = recipes.pasta? .tomatoes;

The operator checks whether the value is filled and then saves the number of tomatoes. The new extension can be called up as often as required if several levels are to be checked. The call chain breaks off if one of the elements is not filled and returns a. It is also possible to use the operator on an array.

const shopping list = ['eggs', 'milk'];
const pasta = shopping list?. [7];

In this case, the optional chaining operator can be used to access the index of an array without knowing whether the array or the specific index is present. The behavior is the same for functions.

print.recipes?. ('soup');