JavaScript nullish coalescing operator

In JavaScript, the nullish coalescing operator ?? is used to provide a default value for a variable if its value is null or undefined.

For example, if we have a variable B and we want to provide a default value of an empty array [] if B is null or undefined, we can use the nullish coalescing operator like this:

const A = B ?? [];

In this case, if B is null or undefined, A will be assigned the value of an empty array []. 

Otherwise, A will have the same value as B.

Leave a Comment

Your email address will not be published. Required fields are marked *