Introduction to JavaScript conditional access attributes and arrow functions

Introduction to JavaScript conditional access attributes and arrow functions

1. Conditional access attributes

?. is a new feature introduced in ES2020. It is a conditional property access operator. When you access a property value of an undefined variable, if you use the . operator, an error will be reported directly. If you use the conditional property access operator to access it, undefined will be returned.

See the example:

let book = {price:10,
            edition:10,
            name:"javascript"
}

console.log(book.page.num)

Direct error reporting:

TypeError: Cannot read property 'num' of undefined

Because the value of book.page undefined , undefined does not have any attribute value, an error will be reported.

If you are not sure whether a value is undefined or an object, in addition to using the if statement to judge, you can also directly use the conditional access operator to access a property. Even if the object being accessed is undefined , no error will be reported. Instead, it returns undefined

console.log(book.page?.num)

Output:

undefined

2. Introduction to Arrow Functions

Arrow function is a shorthand method for defining functions that appeared in ES6 , using => to separate the parameter list and the function body.

example:

let square = x=>x**2;

console.log(square(3))

Output:

9

The definition of this function is equivalent to the traditional function:

function square(x){
    return x**2
}

Arrow functions are often used to pass an unnamed function as a parameter to another function.

let nums = [1,2,3,4].map(x=>x*2)

console.log(nums)

Output:

[ 2, 4, 6, 8 ]

Arrow functions make the code look more concise.

If you use the traditional function keyword to define a function, it will seem a bit verbose.

nums = [1,2,3,4].map(function(x){return x*2})

console.log(nums)

If an arrow function has multiple parameters, they need to be enclosed in parentheses.

const add = (x,y)=>x+y;

console.log(add(1,2))

If the body of an arrow function has multiple statements, enclose the body in curly braces and use the return keyword to return the value.

const add = (x,y)=>{let tmp=x+y;return tmp};

console.log(add(1,2))

At this time, the function body of the arrow function is exactly the same as the function body format of the ordinary function definition. So arrow functions are concise and readable in simple single-line statements. Once the function body is too complex, using arrow functions to define it is not so readable.

This is the end of this article about JavaScript conditional access to properties and arrow functions. For more relevant JavaScript conditional access to properties and arrow functions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Tips for writing better JavaScript conditionals and matching conditions (summary)
  • Detailed explanation of this pointing in JS arrow function
  • Characteristics of JavaScript arrow functions and differences from ordinary functions
  • Detailed explanation of the difference between arrow functions and normal functions in JavaScript
  • What scenarios are not suitable for JS arrow functions?
  • Which scenarios in JavaScript cannot use arrow functions

<<:  The best solution for implementing digital plus and minus buttons with pure CSS

>>:  Button does not specify type as submit. Clicking the button does not jump to the specified URL.

Recommend

Implementation of mysql decimal data type conversion

Recently, I encountered a database with the follo...

React handwriting tab switching problem

Parent File import React, { useState } from '...

Basic ideas and codes for implementing video players in browsers

Table of contents Preface Summary of audio and vi...

Interactive experience trends that will become mainstream in 2015-2016

The most important interactive design article in ...

Brief Analysis of MySQL B-Tree Index

B-Tree Index Different storage engines may also u...

MySql Group By implements grouping of multiple fields

In daily development tasks, we often use MYSQL...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

Install Zookeeper under Docker (standalone and cluster)

After starting Docker, let's take a look at t...

How to maintain MySQL indexes and data tables

Table of contents Find and fix table conflicts Up...

MySQL database terminal - common operation command codes

Table of contents 1. Add users 2. Change the user...

Code for implementing simple arrow icon using div+CSS in HTML

In web design, we often use arrows as decoration ...

Mybatis paging plug-in pageHelper detailed explanation and simple example

Mybatis paging plug-in pageHelper detailed explan...