Jun 02, 2021 Article blog
To determine whether a programmer is a good programmer, look at the code he writes and you can know a thing or two, because the most important thing for a programmer is to write code.
Good programmers write code that is neat and regulated, and visually naturally has a sense of beauty. Good programmers adopt consistent shorthand rules, while poor programmers sometimes don't shorthand, sometimes shorthand.
Today w3cschool editor-in-chief I put together a list of 10 programmers commonly used code shorthand technology, so that your code book shorthand neat specification has a sense of beauty, let's learn about these codes!
1, object properties shorthand
If the property name is the same as the key name, you can use ES6:
const obj = { x:x, y:y };
Shorthand:
const obj = { x, y };
2, declare the variable shorthand method
let x; l et y; let z = 3;
Shorthand:
let x, y, z=3;
3, troy operator
When you want to write if... When you use a mitso operator instead of an else statement.
const x = 20; l et answer; if (x > 10) {
Shorthand:
const answer = x > 10 ? 'is greater' : 'is lesser';
Of course, if statements can also be nested:
const big = x > 10 ? " greater 10" : x
4, short-circuit evaluation of shorthand
When assigning another value to a variable, if we want to determine that the source start value is not null, undefined, or empty, we can use the short-circuit evaluation method:
const variable2 = variable1 || 'new';
You can write an if statement that is composed with multiple criteria.
5, if the existence of conditional shorthand method
if (likeJavaScript === true)
Shorthand:
if (likeJavaScript)
The statements are equal only if the like JavaScript is true
If the judgment value is not true, you can do this:
let a; if ( a !== true ) {// do something...}
Shorthand:
let a; if ( !a ) {// do something...}
6, implicitly return value shorthand
Return statements are often used to return the final result of a function, and an arrow function with a separate statement implicitly returns its value (the function must be omitted).
In order to omit the return keyword) to return a multi-line statement, such as an object literal expression, you need to surround the function body with ().
Shorthand:
7, JavaScript loop shorthand method
for (let i = 0; i < allImgs.length; i++)
Shorthand:
for (let index in allImgs)
You can also use Array.forEach:
8, short-circuit evaluation
A variable is assigned a value by determining whether its value is null or underfined, you can:
let dbHost; if (process.env.DB_HOST) {
Shorthand:
const dbHost = process.env.DB_HOST || 'localhost';
9, decimal index
When you need to write a number band with a lot of zeros (e.g. 10000000), you can replace that number with an index (1e7):
for (let i = 0; i < 10000; i++) {}
Shorthand:
10, the arrow function shorthand
Traditional function writing methods are easy to understand and write, but if nested in another function, these advantages disappear.
function sayHello(name) { console.log('Hello', name);
Shorthand:
sayHello = name => console.log('Hello', name);