JavaScript Optional Chaining Last Updated : 17 May, 2025 Comments Improve Suggest changes 5 Likes Like Report JavaScript Optional chaining is a new feature introduced in ES2020 that simplifies the process of accessing properties nested within objects, especially when dealing with potentially null or undefined values. It provides a concise syntax for accessing properties deep within nested objects without encountering errors due to null or undefined values. This helps in writing cleaner and more readable code by reducing the need for explicit null checks and conditionals.When we want to check a value of the property that is deep inside a tree-like structure, we often have to check whether intermediate nodes exist.let Value = user.dog && user.dog.name;The Optional Chaining Operator allows a developer to handle many of those cases without repeating themselves by assigning intermediate results in temporary variables:let Value = user.dog?.name;Syntax: obj?.propobj?.[expr]arr?.[index]func?.(args)Note: If this code gives any error try to run it on online JavaScript editor.Example: To demonstrate the implementation of the Optional Chaining with Object in JavaScript. JavaScript const user = { dog: { name: "Alex" } }; console.log(user.cat?.name); //undefined console.log(user.dog?.name); //Alex console.log(user.cat.name); Output:Example: To demonstrate the Optional Chaining with Function Call in JavaScript. JavaScript let user1 = () => console.log("Alex"); let user2 = { dog() { console.log("I am Alex"); } } let user3 = {}; user1?.(); // Alex user2.dog?.(); // I am Alex user3.dog(); // ERROR - Uncaught TypeError: // user3.dog is not a function. user3.dog?.(); // Will not generate any error. Output: Create Quiz Optional Chaining Visit Course Comment A abhishekkharmale Follow 5 Improve A abhishekkharmale Follow 5 Improve Article Tags : JavaScript Web Technologies JavaScript-Misc JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like