Swizec Teller - a geek with a hatswizec.com

    A surprising feature of JavaScript optional chaining

    In 2020 JavaScript gained a new feature – optional chaining. It solves a problem we've had ... forever. With many workarounds and standard solutions over the years.

    Swizec Teller published ServerlessHandbook.dev avatarSwizec Teller published ServerlessHandbook.dev@Swizec
    TIL you can use optional chaining on function calls 🤯
    Tweet media

    The problem optional chaining solves

    You get an object like this:

    const object = {
    greet: "hai",
    deepProp: {
    greet: "hello",
    deeperProp: {
    greet: "ohai",

    JSON response from an API or reading from a database. Perhaps a blob you've built up on the frontend.

    How do you access the 3rd level greet, if object, deepProp, and deeperProp might be undefined?

    You could rely on JavaScript's evaluation semantics. Last value from expression is returned.

    const greeting =
    object &&
    object.deepProp &&
    object.deepProp.deeperProp &&

    Confusing for newbies, annoying to write, easy to get wrong. Exploding complexity to boot.

    A clearer way to write that are conditionals:

    let greeting = undefined
    if (object) {
    if (object.deepProp) {
    if (object.deepProp.deeperProp) {
    greeting = object.deepProp.deeperProp

    Clearer, more verbose, nobody writes this in production code. Feels weird to use conditionals for assignment. 🤷

    Another common approach is to use a library like Lodash:

    const greeting = _.get(object, "deepProp.deeperProp.greet")

    Personally not a fan. Feels unnecessary.

    Use optional chaining

    With optional chaining you can do this natively:

    const greeting = object?.deepProp?.deeperProp?.greet


    You've probably seen that before. Everyone's been very excited after wishing this existed for 10+ years.

    But you might've missed that the operator is ?., not ?. This is important because you can optionally chain anything 🤯

    Function calls:


    Array access:


    And even expressions:

    object?.deepProp?.[console.log("runs if deepProp defined")]

    Please don't use that last one 😅


    Did you enjoy this article?

    Published on June 24th, 2021 in JavaScript, Technical

    Learned something new?
    Want to become a high value JavaScript expert?

    Here's how it works 👇

    Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Start with an interactive cheatsheet 📖

    Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

    "Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created bySwizecwith ❤️