In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.