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

Usage of Linux userdel command

1. Command Introduction The userdel (user delete)...

14 practical experiences on reducing SCSS style code by 50%

Preface Sass is an extension of the CSS3 language...

Detailed explanation of the four transaction isolation levels in MySQL

The test environment of this experiment: Windows ...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Vue+Element UI realizes the encapsulation of drop-down menu

This article example shares the specific code of ...

nginx automatically generates configuration files in docker container

When a company builds Docker automated deployment...

How to use Navicat to operate MySQL

Table of contents Preface: 1. Introduction to Nav...

【HTML element】Detailed explanation of tag text

1. Use basic text elements to mark up content Fir...

The button has a gray border that is ugly. How to remove it?

I used the dialog in closure and drew a dialog wit...

mysql replace part of the field content and mysql replace function replace()

[mysql] replace usage (replace part of the conten...

MySQL database table partitioning considerations [recommended]

Table partitioning is different from database par...

js to realize automatic lock screen function

1. Usage scenarios There is such a requirement, s...

Solution for Baidu site search not supporting https (tested)

Recently, https has been enabled on the mobile ph...