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 See the example: let book = {price:10, edition:10, name:"javascript" } console.log(book.page.num) Direct error reporting:
Because the value of If you are not sure whether a value is console.log(book.page?.num) Output:
2. Introduction to Arrow Functions Arrow function is a shorthand method for defining functions that appeared in example: let square = x=>x**2; console.log(square(3)) Output:
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:
Arrow functions make the code look more concise. If you use the traditional 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 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:
|
<<: 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.
1. Command Introduction The userdel (user delete)...
Preface Sass is an extension of the CSS3 language...
The test environment of this experiment: Windows ...
Table of contents cause reason Introduction to NP...
This article example shares the specific code of ...
When a company builds Docker automated deployment...
Table of contents Preface: 1. Introduction to Nav...
1. Download nginx [root@localhost my.Shells]# doc...
1. Use basic text elements to mark up content Fir...
I used the dialog in closure and drew a dialog wit...
[mysql] replace usage (replace part of the conten...
Table partitioning is different from database par...
1. Usage scenarios There is such a requirement, s...
Recently, https has been enabled on the mobile ph...
I encountered this problem when I was making the ...