Twelve commonly used javascript abbreviation techniques - can greatly reduce the amount of js code

Keywords: Javascript less Programming Google

Wechat Public Number Personal Blog Know about

This article is not written by myself, but I think it is very helpful to me. So I share it with you. The original link is at the bottom. Thank you for watching.
1. Null (undefined) verification
When we create a new variable, we usually verify that the value of the variable is null or undefined. This is a validation often considered for JavaScript programming.
If written directly, as follows:

if (variable1 !== null || variable1 !== undefined || variable1 !== ''){
   let variable2 = variable1;
}

We can use a simpler version.

let variable2 = variable1 || '';

If you don't believe it, you can try it on the console in Google Browser Developer mode!

//An example of a value of null
let variable1 = null;
let variable2 = variable1 || '';
console.log(variable2);
//Output:''
//An example with undefined value
let variable1 = undefined;
let variable2 = variable1 || '';
console.log(variable2);
//Output:''
//Normal Conditions
let variable1 = 'hi there';
let variable2 = variable1 || '';
console.log(variable2);
//Output:'hi there'

Note here that after debugging a set of code, you need to refresh the next page, or define different variables, or you will report errors:

2. Arrays
This seems to be relatively simple!
Non-optimized code:

let a = new Array(); a[0] = "myString1"; a[1] = "myString2"; a[2] = "myString3";

Optimize the code:

let a = ["myString1", "myString2", "myString3"];

3. If true.. Else optimization

let big;
if (x > 10) {
big = true;
}
else {
big = false;
}

After simplification:

let big = x > 10 ? true : false;

This is a trinomial operation, which can be used when there is only one judgment condition and one result.
It greatly simplifies the amount of code!

let big = (x > 10);
let x = 3,
big = (x > 10) ? "greater 10" : (x < 5) ? "less 5" : "between 5 and 10";
console.log(big); //"less 5"
let x = 20,
big = {true: x>10, false : x< =10};
console.log(big); //"Object {true=true, false=false}"

4. Variable declaration
Although JavaScript automatically talks about hoist, using this method, you can say that all variables are fixed in one line at the head of the function.
Optimize money:

let x;
let y;
let z = 3;

After optimization:

let x, y, z=3;

5. Simplification of assignment statements
Before simplification:

x=x+1;
minusCount = minusCount - 1;
y=y*10;

After simplification:

x++;
minusCount --;
y*=10;

Assuming x=10 and y=5, basic arithmetic operations can be abbreviated as follows:

x += y // x=15
x -= y // x=5
x *= y // x=50
x /= y // x=2
x %= y // x=0

6. Avoid using RegExp objects
Before simplification:

var re = new RegExp("\d+(.)+\d+","igm"),
result = re.exrc("padding 01234 text text 56789 padding");
console.log(result);//"01234 text text 56789"

After simplification:

var result = /d+(.)+d+/igm.exec("padding 01234 text text 56789 padding");
console.log(result); //"01234 text text 56789"

7. If condition optimization
Before simplification:

if (likeJavaScript === true)

After simplification:

if (likeJavaScript)

Let's take another example to judge the truth:

let c;
if ( c!= true ) {
// do something...
}

After simplification:

let c;
if ( !c ) {
// do something...
}

9. Function parameter optimization
Personally, I prefer to access function parameters by acquiring object elements. Of course, there are different opinions.
Usually used versions:

function myFunction( myString, myNumber, myObject, myArray, myBoolean ) {
// do something...
}
myFunction( "String", 1, [], {}, true );

My favorite version:

function myFunction() {
/* Comment Section
console.log( arguments.length ); // Return 5
for ( i = 0; i < arguments.length; i++ ) {
console.log( typeof arguments[i] ); // Return string, number, object, Boolean
}
*/
}
myFunction( "String", 1, [], {}, true );

Translator's Note: A comment below the original text indicates that it is not recommended to use the landlord's method. The order of function parameters can be changed by using the first method, and you should be careful about the second one.
10. Alternatives to charAt ()
Before simplification:

"myString".charAt(0);

After simplification:

"myString"[0];//Return to'm'

Translator's Note: I believe there are not many people using the first method.
11. Function calls can also be shorter
Before simplification:

function x() {console.log('x')};function y() {console.log('y')};
let z = 3;
if (z == 3)
{
x();
} else
{
y();
}

After simplification:

function x() {console.log('x')};function y() {console.log('y')};let z = 3;
(z==3?x:y)();

12. How to express big numbers gracefully
In JavaScript, there's a way to abbreviate numbers that you may have overlooked. 1e7 means 10000000.
Before simplification:

for (let i = 0; i < 10000; i++) {

After simplification:

for (let i = 0; i < 1e7; i++) {

Translator: Fundebug
Translation: http://www.cnblogs.com/fundeb...
Original: https://hackernoon.com/12-ama...

Posted by kkeim on Wed, 05 Jun 2019 12:31:47 -0700