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 ten most commonly used code shorthand techniques in JavaScript

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

Share

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

This article mainly introduces "what are the ten most commonly used code shortening techniques in JavaScript". In daily operation, I believe many people have doubts about what are the ten most commonly used code shortening techniques in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts about "what are the ten most commonly used code shortening techniques in JavaScript?" Next, please follow the editor to study!

1. Shorthand of object attributes

If the property name is the same as the key name, you can use the ES6 method:

Const obj = {XRV x, YRV y}

Abbreviation:

Const obj = {x, y}

2. The shorthand method of declaring variables

Let xterlet ytterlet z = 3

Abbreviation method:

Let x, y, zonal 3

3. Ternary operator

When you want to write if... Use ternary operators instead of else statements.

Const x = 20 answer;if (x > 10) {

Abbreviation:

Const answer = x > 10? 'is greater':'is lesser'

Of course, you can also nest if statements:

Const big = x > 10? "greater 10": X

4. Shorthand method of short circuit evaluation

When assigning another value to a variable, we can use the short-circuit evaluation method if we want to determine that the source value is not null,undefined or null:

Const variable2 = variable1 | | 'new'

You can write an if statement with multiple conditions.

5. The abbreviation method of if existence condition.

If (likeJavaScript = true)

Abbreviation:

If (likeJavaScript)

Only when likeJavaScript is true, the two statements are equal.

If the judgment value is not true, you can do this:

Let an if (a! = = true) {/ / do something...}

Abbreviation:

Let a! if (! a) {/ / do something...}

6. Abbreviation of implicit return value

The return statement is often used to return the final result of a function, and the arrow function of a single statement can implicitly return its value (the function must be omitted

{} to omit the return keyword) to return multiline statements (such as object literal expressions), you need to surround the function body with ().

Abbreviation:

7. Shorthand method of JavaScript loop

For (let I = 0; I

< allImgs.length; i++)   简写:   for (let index in allImgs)   也可以使用Array.forEach:

8. Short circuit evaluation

To assign a value to a variable by determining whether its value is null or undefined, you can:

Let dbHost;if (process.env.DB_HOST) {

Abbreviation:

Const dbHost = process.env.DB_HOST | | 'localhost'

9. Decimal index

When you need to write a number with a lot of 00:00 (such as 10000000), you can use an 1e7 instead of this number:

For (let I = 0; I

< 10000; i++) {}   简写:   10、箭头函数简写   传统函数编写方法让人非常容易理解和编写,但是倘若是嵌套在另一个函数中,这些优势就会消失。   function sayHello(name) { console.log('Hello', name);   简写:   sayHello = name =>

Console.log ('Hello', name)

At this point, the study on "what are the ten most commonly used code shortening techniques in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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