
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Strange syntax, what does `?.` mean in JavaScript?
Let’s try to understand ‘?.’ with an example.
Consider the following object example describing a male human being of age 23 −
const being = { human: { male: { age: 23 } } };
Now let’s say we want to access the age property of this being object. Pretty simple, right? We will just use chaining to access like the below code −
Example
const being = { human: { male: { age: 23 } } }; console.log(being.human.male.age);
Output
Console output is as follows −
23
Now let’s say you change the property male of being object to female or something else for some technical reasons.
Now what happens to our chaining statement, it will yield a typeError saying cannot access property undefined of human. Does there exist any way in such situations to make our code not throw any errors. Yes, and this is where ‘?.’ aka optional chaining comes to our rescue.
What optional chaining does is quite simple, it behaves like normal chaining in normal conditions but when we try to access any property off of undefined instead of making our code to throw an error, it terminates the chaining then and there and returns undefined so that the remaining chunk of code functions normally.
Consider our last example (optional chaining) −
Example
const being = { human: { male: { age: 23 } } } console.log(being?.human?.female?.age);
Output
Instead of throwing the error, the output will be −
undefined
- Related Articles
- What are syntax errors in JavaScript?
- What does geometry mean?
- What does psychology mean?
- What does humus mean?
- What does createdCollectionAutomatically mean in MongoDB?
- What does # mean in Lua programming?
- What does operator ~= mean in Lua?
- What does series mean in pandas?
- What does "return@" mean in Kotlin?
- What does NFT mean in Blockchain?
- What does opt mean in Linux
- What does “javascript:void(0)” mean?
- What does these operators mean (** , ^ , %, //) ?
- What Does Defensive Security Mean?
- What Does Offensive Security Mean?
