JavaScript ES2020 - The Most Requested Feature Explained in 10 Minutes

JavaScript Mastery · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

The video explains the Optional Chaining feature in JavaScript ES2020, which allows accessing deeply nested object properties without worrying about property existence, and demonstrates its usage with examples.

Full Transcript

great news the new es 20/20 features are now finalized this means we now have a complete idea of the changes happening in ES 2020 the new and improved specification of JavaScript so what are the changes as you can see there aren't many there are some but in this video we are going to explore in my opinion the most important feature of ES 2020 which is optional chaining let's dive right in [Music] optional chaining is the feature I'm most excited about in ES 2020 optional chaining syntax allows you to access deeply nested object properties without worrying if the property exists or not if it exists great if it doesn't we're just going to return undefined super convenient currently if we want to access a deeply nested property of an object we have to check if the property in each nesting level is defined by using logical expressions what does all of that mean and why should we care the best way to see it is in an example so let me show you the current problem that we have let's create a basic object so let's do Const user and that's going to be equal to an object which has the property of first name let's say John and also a property of last name which is going to be Doe in there we also have the address let's say the city is NYC the street is some kind of road let's say test road after that we have a house which is going to be another object and then in there we have a house number let's say five this is our finished object now if we have a nested object inside of an object that is undefined or null in any level then our program will crash and the rest won't even run if we don't do a check for that this means we have to check every level to make sure that it won't crash when it runs into an undefined or novel so let's say that we want to retrieve the house number that's going to be easier right console.log that's going to be user that address that house and then that number this is going to give us the number which is going to be fine pretty straightforward but let's say that we are fetching this data from the API and for some unfortunate reason we don't even get the address then the object would look like this so we are going to comment this out and the object which is not fetched properly is going to look like this let's say so the address is going to be completely undefined with that in mind what would our previous console log this one here return do you think it'll be undefined so right now we are going to copy this code and we are going to head to the browser so this is just the ambient page for optional chaining you can also check it out and in here I just have the console open you can right click and then inspect and open the console if you don't know how to so right there I'm going to paste our example it's not going to be 5 anymore of course and you can see what happens looks like we got an error what does that mean for our application the entire code execution stopped app trashed we never want this to happen solution for this problem before is 20/20 looks just awful we first have to check whether user object exists and we can do that like this user and end this is going to make sure that the user evaluates to true D then we have to check whether user that address exists so we can do that by user that address so if the user exists then check for the user that address if user that address exists then check for user that address and then that home or that was house you can see that right there and then finally if that exists then try to console log the number of the house so if we now take this go back to the browser again and refresh as you can see we are getting undefined nothing special we don't get any value but the main point of this is that we indeed do get undefined and the air is not shown our application didn't crash so to be able to achieve that we had to do this ugly code we can even bring it in one line but still doesn't look all that better with this approach we wrote many lines of code and we accomplished to bypass the error and get only undefined so far this has been the only way to solve this without getting an error now we are going to dive into the es 20/20 solution with optional chaining with the optional chaining operator we just need to use the question mark dot notation question mark dot looks a bit odd but does the new syntax to access nested objects and if it bumps into a property that's null or undefined then it just returns undefined so if it bumps into now or undefined returns undefined so how can we show that in action we don't need this we are just going to get this console log right there remember if we just can't select like this we are getting an error because we don't have any properties here and we cannot read the number of the house which doesn't exist and we cannot even read the house of the address which is not an object take a look at this question mark dot or the optional chaining I'm going to even write it here optional chaining yes 20/20 so before the dot we just add question mark this is going to make sure that the user exists and then we can move on to the address then we are going to add another question mark here and if that exists finally we can check for the number that's it this is our solution with this we are successfully getting undefined instead of an error let's copy this and head to the browser so if you copy it and then refresh the browser ignore these errors and warnings and we can just base the code as you can see we indeed God undefined and no error was shown when our program runs into our undefined or null in this case address is the undefined so it's right there instead of crashing we'll just get back undefined otherwise the return value will be the value of the property you wanted to access as expected let's test that out instead of this user I'm going to bring back our original properly fetched user now if we copy this go back refresh and then paste it you'll be able to see that we should get back the number five as we expect and we do we don't have to write a lot of logical expressions just to be able to access a deeply nested object the final thing you might be wondering about is how we do the dynamic keys so in objects you can have for example a key of house and the number you want to access but sometimes you don't have this as a string so let's say you want to do Const searched property and then in here you get the actual thing you're looking for in this case we just have it as a constant but sometimes you're going to get this value dynamically from an input for example or from anywhere else so in this case we have to check this thing let me show you what I mean when I say dynamic key so let's say that we want to access this console.log user that address that house and then in here we want to access the search property which is in this case going to be a number so how can we use a question mark dot notation with this in here we don't have the dot we just have the square brackets so what can we do for these things is the same thing right there right there and you might think this is going to work but we indeed do need the question mark and the dot for this to work and that's it if we just try it out and we try it on for example let's say this user which doesn't have an address we can save it copy that and go back into the browser refresh and if we baste it as you can see we do get undefined instead of an error which means that the optional chaining operator works successfully that's going to be it for this video if you'd like more is 20/20 feature videos let me know in the comments which one you would like and subscribe to see them soon have a great day and stay safe [Music]

Original Description

Good news – the new ES2020 features are now finalized! This means we now have a complete idea of the changes happening in ES2020, the new and improved specification of JavaScript. In this video, we're going to explore, in my opinion, the most important feature of ES2020 which is Optional Chaining. Optional chaining syntax allows you to access deeply nested object properties without worrying if the property exists or not. If it exists, great! If not, undefined will be returned. Super convenient! ⭐ Join JS Mastery Pro: https://jsmastery.com 💎 Ultimate Next.js Course: https://jsmastery.com/course/the-ultimate-next-js-15-course 🧪 Ultimate Testing Course: https://jsmastery.com/course/the-complete-next-js-testing-course 📗 GSAP Course: https://jsmastery.com/course/gsap-animations-course 📕 Three.js 3D Course: https://jsmastery.com/course/vanilla-three-js-course 📙 JavaScript Course: https://jsmastery.com/course/complete-path-to-javascript-mastery 🚀 Launch Your SaaS Course: https://jsmastery.com/course/build-launch-your-saas-in-under-7-days 📚 Materials/References: Optional Chaining MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining 💻 Discord - https://discord.gg/n6EdbFJ 🐦 Twitter - https://twitter.com/jsmasterypro 🖼️ Instagram - https://instagram.com/javascriptmastery 💼 Business Inquiries: contact@jsmastery.pro
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from JavaScript Mastery · JavaScript Mastery · 20 of 60

1 Learn Async/Await in This Real World Project
Learn Async/Await in This Real World Project
JavaScript Mastery
2 JavaScript Exercise | Learn JavaScript with Exercism | #0 Setup
JavaScript Exercise | Learn JavaScript with Exercism | #0 Setup
JavaScript Mastery
3 JavaScript ES6 for Beginners
JavaScript ES6 for Beginners
JavaScript Mastery
4 ES7 and ES8 New Features
ES7 and ES8 New Features
JavaScript Mastery
5 Learn JSON in a Real World React App
Learn JSON in a Real World React App
JavaScript Mastery
6 How to Create PDFs With Node JS and React
How to Create PDFs With Node JS and React
JavaScript Mastery
7 Must Have Visual Studio Code Extensions
Must Have Visual Studio Code Extensions
JavaScript Mastery
8 Top 10 JavaScript Array Methods
Top 10 JavaScript Array Methods
JavaScript Mastery
9 JavaScript Map and Set Explained
JavaScript Map and Set Explained
JavaScript Mastery
10 Git Commands Tutorial for Beginners
Git Commands Tutorial for Beginners
JavaScript Mastery
11 Build and Deploy a YouTube Clone Application Using React
Build and Deploy a YouTube Clone Application Using React
JavaScript Mastery
12 React Hooks - Most Used Features
React Hooks - Most Used Features
JavaScript Mastery
13 JavaScript Best Practices and Coding Conventions - Write Clean Code
JavaScript Best Practices and Coding Conventions - Write Clean Code
JavaScript Mastery
14 Build and Deploy a Realtime Chat Application - Socket.io, Node.js, and React.js
Build and Deploy a Realtime Chat Application - Socket.io, Node.js, and React.js
JavaScript Mastery
15 How to Create and Deploy a Portfolio Site in less than 30 Minutes
How to Create and Deploy a Portfolio Site in less than 30 Minutes
JavaScript Mastery
16 SEO for Developers | 2020 SEO Tutorial
SEO for Developers | 2020 SEO Tutorial
JavaScript Mastery
17 Web Development Roadmap 2020 [Learning Path] - Start Coding at Home!
Web Development Roadmap 2020 [Learning Path] - Start Coding at Home!
JavaScript Mastery
18 Random Quote Generator - React Fetch API Data | Build and Deploy a Real Advice App Project
Random Quote Generator - React Fetch API Data | Build and Deploy a Real Advice App Project
JavaScript Mastery
19 Build a COVID-19 Tracker Application - React JS Project (Hooks, Material UI, Charts js)
Build a COVID-19 Tracker Application - React JS Project (Hooks, Material UI, Charts js)
JavaScript Mastery
JavaScript ES2020 - The Most Requested Feature Explained in 10 Minutes
JavaScript ES2020 - The Most Requested Feature Explained in 10 Minutes
JavaScript Mastery
21 Modern React Event Handling Using Hooks
Modern React Event Handling Using Hooks
JavaScript Mastery
22 Deno JS - Intro +  Real Life Example
Deno JS - Intro + Real Life Example
JavaScript Mastery
23 Build and Deploy a React PWA - Why Progressive Web Apps are the Future of the Web
Build and Deploy a React PWA - Why Progressive Web Apps are the Future of the Web
JavaScript Mastery
24 Build a REST API with Node JS and Express | CRUD API Tutorial
Build a REST API with Node JS and Express | CRUD API Tutorial
JavaScript Mastery
25 Build and Deploy an ARTIFICIAL INTELLIGENCE React App | Alan AI, JavaScript
Build and Deploy an ARTIFICIAL INTELLIGENCE React App | Alan AI, JavaScript
JavaScript Mastery
26 Master Async JavaScript using Async/Await | Quokka JS
Master Async JavaScript using Async/Await | Quokka JS
JavaScript Mastery
27 Spaced Repetition in Programming | mem.dev
Spaced Repetition in Programming | mem.dev
JavaScript Mastery
28 Stop Copy & Pasting Code | mem.dev
Stop Copy & Pasting Code | mem.dev
JavaScript Mastery
29 GitHub Profile README | Create an Amazing Profile Readme | Setup + Templates
GitHub Profile README | Create an Amazing Profile Readme | Setup + Templates
JavaScript Mastery
30 NEW GitHub CLI 1.0 is here! | GitHub CLI Tutorial - Demo & Commands
NEW GitHub CLI 1.0 is here! | GitHub CLI Tutorial - Demo & Commands
JavaScript Mastery
31 React Custom Hooks | Learn Custom Hooks & Build a Project
React Custom Hooks | Learn Custom Hooks & Build a Project
JavaScript Mastery
32 Learn how to deploy an NPM Package
Learn how to deploy an NPM Package
JavaScript Mastery
33 JavaScript Algorithms for Beginners
JavaScript Algorithms for Beginners
JavaScript Mastery
34 Level UP your GitHub Game - Get Hired Quickly
Level UP your GitHub Game - Get Hired Quickly
JavaScript Mastery
35 The Best Way to Host & Deploy a React Application
The Best Way to Host & Deploy a React Application
JavaScript Mastery
36 Full Stack MERN Project - Build and Deploy an App | React + Redux, Node, Express, MongoDB [Part 1/2]
Full Stack MERN Project - Build and Deploy an App | React + Redux, Node, Express, MongoDB [Part 1/2]
JavaScript Mastery
37 Full Stack MERN Project - Build and Deploy an App | React + Redux, Node, Express, MongoDB [Part 2/2]
Full Stack MERN Project - Build and Deploy an App | React + Redux, Node, Express, MongoDB [Part 2/2]
JavaScript Mastery
38 ECommerce Web Shop - Build & Deploy an Amazing App | React.js, Commerce.js, Stripe
ECommerce Web Shop - Build & Deploy an Amazing App | React.js, Commerce.js, Stripe
JavaScript Mastery
39 JavaScript Crash Course 2021 - Master JavaScript in One Video!
JavaScript Crash Course 2021 - Master JavaScript in One Video!
JavaScript Mastery
40 MERN Auth - Login with Email (JWT) + Google OAuth Authentication | React, Node, Express, MongoDB
MERN Auth - Login with Email (JWT) + Google OAuth Authentication | React, Node, Express, MongoDB
JavaScript Mastery
41 Chat Application using React JS - Build and Deploy a Chat App in 1 Hour (Microsoft Teams)
Chat Application using React JS - Build and Deploy a Chat App in 1 Hour (Microsoft Teams)
JavaScript Mastery
42 MUST USE Websites & Tools for Web Developers
MUST USE Websites & Tools for Web Developers
JavaScript Mastery
43 Learn Material UI in One Hour - React Material UI Project Tutorial [2022]
Learn Material UI in One Hour - React Material UI Project Tutorial [2022]
JavaScript Mastery
44 Shopify ECommerce Store with React & Next JS | BuilderIO
Shopify ECommerce Store with React & Next JS | BuilderIO
JavaScript Mastery
45 React Video Chat App | WebRTC Video Chat Zoom Clone | Tabnine
React Video Chat App | WebRTC Video Chat Zoom Clone | Tabnine
JavaScript Mastery
46 TypeScript Crash Course 2021
TypeScript Crash Course 2021
JavaScript Mastery
47 Build and Deploy a Premium Next JS React Website | Landing Page, Business Website, Portfolio
Build and Deploy a Premium Next JS React Website | Landing Page, Business Website, Portfolio
JavaScript Mastery
48 Full Stack MERN Project - Pagination & Search | React + Redux, Node, Express, MongoDB
Full Stack MERN Project - Pagination & Search | React + Redux, Node, Express, MongoDB
JavaScript Mastery
49 Build a BETTER Facebook Messenger Chat Application | React JS, Firebase, Chat Engine
Build a BETTER Facebook Messenger Chat Application | React JS, Firebase, Chat Engine
JavaScript Mastery
50 Build and Deploy THE PERFECT Portfolio Website | Create a Portfolio from Scratch
Build and Deploy THE PERFECT Portfolio Website | Create a Portfolio from Scratch
JavaScript Mastery
51 Full Stack MERN Project - Implement MERN Comments | React + Redux, Node, Express, MongoDB
Full Stack MERN Project - Implement MERN Comments | React + Redux, Node, Express, MongoDB
JavaScript Mastery
52 Turn an API into a Startup?! Build & Sell an API with JavaScript
Turn an API into a Startup?! Build & Sell an API with JavaScript
JavaScript Mastery
53 Exclusive First Look at GitHub Copilot - All you need to know
Exclusive First Look at GitHub Copilot - All you need to know
JavaScript Mastery
54 Build and Deploy a Google Maps Travel Companion Application | React.js
Build and Deploy a Google Maps Travel Companion Application | React.js
JavaScript Mastery
55 Build and Deploy a Full Stack Realtime Chat Messaging App with Authentication & SMS Notifications
Build and Deploy a Full Stack Realtime Chat Messaging App with Authentication & SMS Notifications
JavaScript Mastery
56 Build and Deploy a React Cryptocurrency App and Master Redux Toolkit in One Video
Build and Deploy a React Cryptocurrency App and Master Redux Toolkit in One Video
JavaScript Mastery
57 Build and Deploy a Group Video Chat Application with Messaging, Polls & More
Build and Deploy a Group Video Chat Application with Messaging, Polls & More
JavaScript Mastery
58 Build and Deploy Google Search 2.0 with React & Tailwind CSS (simple!)
Build and Deploy Google Search 2.0 with React & Tailwind CSS (simple!)
JavaScript Mastery
59 Top 10 Web Development Chrome Extensions You Simply Need to Try!
Top 10 Web Development Chrome Extensions You Simply Need to Try!
JavaScript Mastery
60 Build and Deploy THE BEST Modern Blog App with React | GraphQL, NextJS, Tailwind CSS
Build and Deploy THE BEST Modern Blog App with React | GraphQL, NextJS, Tailwind CSS
JavaScript Mastery

The Optional Chaining feature in JavaScript ES2020 allows developers to access nested object properties without worrying about property existence, reducing the need for extensive error checking and making code more concise.

Key Takeaways
  1. Create a nested object
  2. Access a deeply nested property using Optional Chaining
  3. Handle errors with Optional Chaining
  4. Use dynamic keys with Optional Chaining
💡 The Optional Chaining operator (?.) returns undefined if a property is null or undefined, preventing code execution from stopping due to errors.

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
How to Open KIT Files (CodeKit Project)
File Extension Geeks
Watch →