Table of Contents
Learn About the New Optional Chaining Operator (?.) in JavaScript and Its is easier to approach with a clear overview and reliable steps. This guide organizes the essential information, highlights practical details, and explains what to check along the way.
Key Takeaways
- Review introduction to the optional chaining operator (the?. Symbol).
- Explore conclude.
- Follow the recommended steps in order to avoid common mistakes with learn about the new optional chaining operator (?.) in JavaScript and its.
Introduction to the Optional Chaining Operator (the?. Symbol)
Introduced in ES2020, the?. Operator helps solve the problem of accessing properties in an object even if the object or its properties don't exist. Typically, there are 3 syntaxes to use the?. Operator as follows:
- Used to access object properties:
For instance: let abc = data?.obj.abc.
- Used to access array properties:
For instance: let abc = data?.[obj].abc.
- Used to call an object's function even when it is uncertain whether the function exists or not:
For instance: data.method?.()
Applying the?. Operator to the case of displaying nested objects in Vue's templates, we can write it concisely and safely as follows:
{{ data?.user?.name }}
The browser will no longer report the error Error in render: "TypeError: Cannot read property 'name' of undefined". However, in case the data or user property doesn't exist, we will see the undefined value displayed on the browser, which is not good for the user experience. We can set an empty value '' or some default value to replace undefined by adding that value after 2?? At the end of the nested object as follows: This helps you avoid this problem.
- Set empty value ''
{{ data?.user?.name??'' }}
- Set default value 'abc'
{{ data?.user?.name??'abc' }}
We see that the p tag is still rendered and displayed in Vue's template when using a default value in the?. Operator. There is a better way to use the?. Operator and Vue's v-text directive together. In the example above, the p tag will only be rendered and displayed in Vue's template when the value of the name attribute is not undefined or is set to a default value other than empty. It can be seen that v-text is a very good directive of Vue but is overlooked by many people.
Our above example would become.
Checking the browser, we see that in case the data object or user attribute doesn't exist, the browser doesn't report an error, the p tag will only render in case the name attribute has a specific value. The code becomes concise, easy to follow and safe. So clearly, we see that using the?. Operator with Vue's v-text directive to display the value of a nested object is a perfect combination.

Since the?. Operator is a fairly new operator, introduced in ES2020, some older browser versions from before 2019 may not support this operator. In addition, only Vue 3 currently supports this operator, so to be able to use this operator on Vue 2 and safely on older browsers, we need to configure Vue 2 as follows:
- First, install the vue-template-babel-compiler library to compile code using the? Operator. Run the command to install yarn add -D vue-template-babel-compiler.
- Edit vue.config.js file, use vue-template-babel-compiler to help webpack compile.
// vue.config.js file
Module.exports = {
ChainWebpack: config => {
Config.module
.rule('vue')
.use('vue-loader')
.tap(options => {
Options.compiler = require('vue-template-babel-compiler').
Return options
})
}
}
Or can be configured in webpack.config.js file.
// webpack.config.js
Module: {
Rules: [
{
Test: /.vue$/,
Loader: "vue-loader",
Options: {
Compiler: require("vue-template-babel-compiler"),.
},
},
],
}
With the above configuration, we can use the?. Operator in Vue 2. In case we need to write unit tests using Jest for functions or Vue templates that use the?. Operator, we need to configure jest.config to be able to compile in the test environment as follows.
//jest.config.js file
Module.exports = {
Transform: {
'.*.(vue)$': 'vue-jest',
},
Globals: {
'vue-jest': {
TemplateCompiler: {
Compiler: require('vue-template-babel-compiler'),.
TransformAssetUrls: true,
},
},
},
}
Note: vue-jest version must be greater than or equal to 4.0.0 and jest less than or equal to 26.6.3.
Conclude
Although it is a bit cumbersome to configure to be able to use the?. Operator on Vue 2, we have more than enough reasons to do it because of the many benefits and outstanding advantages that the?. Operator brings to us when it is used in Vue as analyzed above.
Final Thoughts
The most reliable way to handle learn about the new optional chaining operator (?.) in JavaScript and its is to follow the process in order, verify each important setting, and test the result before moving on. Use the guidance above as a practical reference, then adjust the details for your device, software version, or specific goal.
FAQ
What should I know first about Learn About the New Optional Chaining Operator (?.) in JavaScript and Its?
In the above example, we want to display the 'name' value of the data object, but if the user property before it doesn't exist, the browser will throw an error Error in render: "TypeError: Cannot read property 'name' of undefined".
How do I get the best results with Learn About the New Optional Chaining Operator (?.) in JavaScript and Its?
Use current software or equipment, follow the steps in order, review the recommended settings, and test one change at a time so you can identify what improves the result.
What should I do if Learn About the New Optional Chaining Operator (?.) in JavaScript and Its doesn't work as expected?
Check compatibility, permissions, connectivity, and version-specific settings. Restart the relevant device or app, then repeat the process carefully before trying a more advanced fix.
Reader Comments 0
Sign in with email or Google to join the discussion.