JS 4 super practical tips to improve development efficiency

JS 4 super practical tips to improve development efficiency

1. Short circuit judgment

This 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 && operator. If the condition before && is false , the code after && will not be executed.

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 key exists in a JS object, because sometimes we are not sure whether the data returned by the background API is correct.

user object contains an object with the attribute name . name object has an attribute firstName . If user.name.firstName is used for direct judgment, an error will be reported if the name attribute does not exist. Therefore, before making the judgment, it is also necessary to determine whether user.name exists, which requires two layers of nested if judgments.

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 user.name does not exist, it will also return false , so we can directly use one layer of judgment.

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 if else nesting

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 if else nesting can simplify the code by removing the else condition because the return statement will terminate the code execution and return to the function.

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:
  • Share some uncommon but useful JS techniques
  • Four practical tips for JavaScript string operations
  • Practical Javascript debugging skills sharing (summary)
  • Summary of some practical tips in JavaScript
  • JavaScript Practical Code Tips
  • Summary of practical tips in vue.js projects
  • 23 practical tips to improve JavaScript execution efficiency
  • AngularJS Practical Development Skills (Recommended)
  • Forty-nine JavaScript tips and tricks

<<:  How to deploy stand-alone Pulsar and clustered Redis using Docker (development artifact)

>>:  Detailed tutorial on installing MySQL database on Alibaba Cloud Server

Recommend

How to install binary MySQL on Linux and crack MySQL password

1. Make sure the system has the required libaio s...

js realizes the image cutting function

This article example shares the specific code of ...

Three ways to configure Nginx virtual hosts (based on domain names)

Nginx supports three ways to configure virtual ho...

Interpretation and usage of various React state managers

First of all, we need to know what a state manage...

The principle and application of MySQL connection query

Overview One of the most powerful features of MyS...

Detailed explanation of how Node.js middleware works

Table of contents What is Express middleware? Req...

How to create a file system in a Linux partition or logical volume

Preface Learn to create a file system on your sys...

XHTML tags have a closing tag

<br />Original link: http://www.dudo.org/art...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

Detailed explanation of HTML tables

Function: data display, table application scenari...

Super detailed basic JavaScript syntax rules

Table of contents 01 JavaScript (abbreviated as: ...

Detailed explanation of the new background properties in CSS3

Previously, we knew several attributes of backgro...

Add a copy code button code to the website code block pre tag

Referring to other more professional blog systems...