1. Short circuit judgmentThis method can be used when only a simple if condition is needed let x = 0; let foo = () => console.log('executed'); if(x === 0){ foo() } The same if function can be achieved by using the let x = 0; let foo = () => console.log('executed'); x === 0 && foo() You can also add more if conditions, but this will also increase the complexity of the statement, and it is not recommended to have more than 2 conditions. let x = 0; let y = 0; let foo = () => console.log('executed'); x === 0 && y === 0 && foo() 2. Optional Chaining Operator (?) We often check whether a let user = { name : { firstName : 'Aofukaosi' } } if(user.name){ if(user.name.firstName){ console.log('user object contains firstName field') } } At this time, we can use the ? operator to simplify the operation. If let user = { name : { firstName : 'Aofukaosi' } } if(user.name?.firstName){ console.log('user object contains firstName field') } 3. Null coalescing operator (??)Compared to if/else, the ternary operator is much shorter. If the logic is simple, it is convenient to use. For example: let user = { name : { firstName : 'Aofukaosi' } } let foo = () => { return user.name?.firstName ? user.name.firstName : 'firstName does not exist' } console.log(foo()) First, use the ? operator to determine whether it exists. If it exists, it returns false. If it does not exist, it returns false. Then proceed to the following logic. Use ?? algorithm to make the code more concise let user = { name : { firstName : 'Aofukaosi' } } let foo = () => { return user.name?.firstName ?? 'firstName does not exist' } console.log(foo()) 4. return termination function The following function determines the value of x, using a lot of let x = 1; let foo = () => { if(x < 1){ return 'x is less than 1' } else { if(x > 1){ return 'x is greater than 1' }else{ return 'x is equal to 1' } } } console.log(foo()) This let x = 1; let foo = () => { if(x < 1){ return 'x is less than 1' } if(x > 1){ return 'x is greater than' } return 'x is equal to 1' } console.log(foo()) This concludes this article about 4 super practical JS tips to improve development efficiency. For more related 4 practical JS tips to improve development efficiency, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to deploy stand-alone Pulsar and clustered Redis using Docker (development artifact)
>>: Detailed tutorial on installing MySQL database on Alibaba Cloud Server
This article shares the installation and configur...
Table of contents 1. Integrate Ant Design Vue 2. ...
1. Download MySQL from the official website: This...
Download the image (optional step, if omitted, it...
Preface Recently, I encountered a requirement at ...
Table of contents 1. Introduction to calculator f...
If you want the application service in the Docker...
Table of contents 1. Description of functions in ...
Table of contents introduction 1. MySQL master-sl...
Table of contents 1. prototype (explicit prototyp...
Table of contents 1. Use the uuid function to gen...
If you want to exit bash, there are two options: ...
Table of contents 1. Bootstrap Grid Layout 2. Ver...
Docker Learning https://www.cnblogs.com/poloyy/p/...
Dockerfile is a text file that contains instructi...