Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the tips for writing short and concise JS code

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what are the tips for writing short and concise JS code?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the tips for writing short and concise JS code"?

Short circuit

Logical operators in Javascript and (& &) can cause a short circuit, such as

Console.log (true & & 0 & & 2) / / 0console.log (true & & 'test' & & 2) / / 2

That is, the code will not continue to run from left to right if it encounters a value that undefined,null,0 and so on will be converted to false.

X = = 0 & & foo () / / is equivalent to if (x = = 0) {foo ()} chain decision operator'?'

Suppose there is an object

Const student = {name: {firstName: 'Joe'}}

We want to do something in the presence of firstname, and we have to check layer by layer.

If (student & & student.name & & student.name.firstName) {console.log ('student First name exists')}

The chain judgment operator will stop and return undefined when a value cannot be obtained at a certain layer.

If (student?.name?.firstName) {console.log ('student First name exists')} Null merge operator'?

We sometimes use ternary operations to simplify if...else... Or return a default value

Const foo = () = > {return student.name?.firstName? Student.name.firstName: 'firstName do not exist'} console.log (foo ())

In this case, we can further simplify the code through null merging

Const foo = () = > {return student.name?.firstName? 'firstName do not exist'} console.log (foo ())

Very similar to the or | operator, but? It only works on undefined and null to avoid the trouble of having a value of 0.

Try to avoid if else nesting

For example

Const foo = () = > {if (x 1) {return'x is greater than 1'} else {return'x is equal to 1'}}

You can make if else nesting less complex by removing the else condition, because the return statement stops code execution and returns the function

Const foo = () = > {if (x1) {return'x is greater than 1'} return'x is equal to 1'}

Good code does not have to be as short as possible, and sometimes oversimplified code will make the debug process more troublesome, so readability is the most important, especially in the case of multi-person cooperation.

At this point, I believe you have a deeper understanding of "what are the tips for writing short and concise JS code?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report