I think the V8 team at Google was waiting for this moment for a long time. The 8th version of the V8 engine (the most popular JavaScript engine) is out! This new version comes with some really nice performance improvements and two new cool JavaScript language features: optional chaining and nullish coalescing.
This is a long-awaited feature. Let’s say you’re working with a terrible API provider. Let’s call this API CrocosAPI. CrocosAPI provides information about all the crocodiles in the world (it’s an unstable API every one knows ‘gators are superior to crocs’).
This is what our function to get a croc’s habitat (some crocodiles live in freshwater, brakish water and/or saltwater)
Now let’s say the developers of CrocosAPI decided to change the structure of their response from:
To:
Now if we call getWaterHabitat
we will get:
TypeError: Cannot read property 'water' of undefined
That’s because crocInfo.habitat
doesn’t exist anymore. Now to access crocInfo
, we have to access crocInfo.environment.water
. This means our entire app will crash just because CrocosAPI’s developers don’t know about versioning. So how to avoid that error? Optional chaining of course!
You can also use optional indexing with arrays:
… And with functions!
…And with dynamic property access. Wow, it really is holiday season ⛄🎄🎁 (at the time of the writing)!
No more type errors anymore, just an undefined
value!
As a quick PSA, don’t rely on optional chaining as an excuse not to do proper error handling. The good thing about the TypeError we get from accessing the property of an undefined value is that:
We should still have some sort of fallback or warning mechanism when trying to access the property of an undefined value.
??
… No I’m not confused, ??
is the new short-circuit operator joining the &&
and ||
family. If you wrote some React, Vue or Angular you have probably already written or seen something like this.
This code will assign the value stored in props.name
if it’s not falsy. If the value is falsy, the value name
will equal CrocName Error
.
But let’s say that for crocodiles who still haven’t been named, the API returns an empty string. In JavaScript, an empty string is considered falsy so this will happen:
These are not the results we were expecting! We want to separate the scenario where props.name
has a null
or undefined
value to the case where props.name
is an empty string. We want age
to equal 0
and isFemale
to be false
. That’s where ??
comes to the rescue.
||
checks if the left hand side operator is falsy. ??
only checks if it is null
or undefined
. Here is a little cheat sheet for you:
You can also mix operators! Just remember to use parenthesis. Try to think about what this would do:
Let’s look at the result of a few values:
props.name === 'Barry'
: crocName === 'Barry'
props.name === ''
: crocName ==== 'Anonymous Croc'
props.name === undefined
: crocName ==== 'CrocName Error'
You might have thought of cool ways to use these two features together!
Nowadays we are spoiled with how fast JavaScript is and even more spoiled by recurrent performance updates. Once again, V8’s engineers improved the performance and memory of their engine. If you’re interested in learning more about how that is, you can check out their release post. The thing I love about these updates is that it improves our code performance, but we don’t have to write anything new!
To check if you can use V8 v8 in Node.jd you can run node -p process.versions.v8
and see if the version is over 8. For now you should use polyfills like core-js on the web and/or a transpiler. If you’re using Babel, @babel/plugin-proposal-optional-chaining
, @babel/plugin-proposal-nullish-coalescing-operator
are available.
Have fun and happy holidays! 🎄🎉
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!