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
1. Make sure the system has the required libaio s...
This article example shares the specific code of ...
Nginx supports three ways to configure virtual ho...
First of all, we need to know what a state manage...
Overview One of the most powerful features of MyS...
Table of contents What is Express middleware? Req...
Preface Learn to create a file system on your sys...
<br />Original link: http://www.dudo.org/art...
[LeetCode] 176. Second Highest Salary Write a SQL...
Function: data display, table application scenari...
Table of contents 1. Download MySQL msi version 2...
1. Find a suitable version of redis for docker Yo...
Table of contents 01 JavaScript (abbreviated as: ...
Previously, we knew several attributes of backgro...
Referring to other more professional blog systems...