JavaScript Optional Chaining Last Updated : 17 May, 2025 Comments Improve Suggest changes Like Article 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: Comment More infoAdvertise with us Next Article JavaScript Optional Chaining A abhishekkharmale Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc JavaScript-Questions Similar Reads Optional Class | Guava | Java Introduction : Optional is an immutable object that may contain a non-null reference to another object. Each instance of this type either contains a non-null reference, or contains nothing, in which case we say that the reference is absent. It is never said to contain null. Example : Hashmap.get(key 4 min read Chain of Responsibility Design Pattern in Java The Chain of Responsibility Design Pattern in Java is a behavioral design pattern that allows an object to pass a request along a chain of handlers. Each handler in the chain decides either to process the request or to pass it along the chain to the next handler. Important Topics for Chain of Respon 9 min read Builder Method Design Pattern in Java Method Chaining: In java, Method Chaining is used to invoke multiple methods on the same object which occurs as a single statement. Method-chaining is implemented by a series of methods that return the this reference for a class instance.Implementation: As return values of methods in a chain is this 5 min read Optional stream() method in Java with examples The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Paramete 2 min read Optional ifPresentOrElse() method in Java with examples The ifPresentOrElse(Consumer, Runnable) method of java.util.Optional class helps us to perform the specified Consumer action the value of this Optional object. If a value is not present in this Optional, then this method performs the given empty-based Runnable emptyAction, passed as the second param 2 min read Like