Home > Article > Web Front-end > Abbreviation skills that JavaScript developers need to know (primary)
This article brings you the abbreviation skills that JavaScript developers need to know (primary part). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The following is a good example of abbreviating a complete if statement into one line of code.
const x = 20; let answer; if (x > 10) { answer = 'greater than 10'; } else { answer = 'less than 10'; }
The abbreviation is:
const answer = x > 10 ? 'greater than 10' : 'less than 10';
The following abbreviation will be very useful when using pure JavaScript (without relying on external libraries, such as jQuery or lodash) .
for (let i = 0; i <p>The abbreviation is: </p><pre class="brush:php;toolbar:false">for (let index of allImgs)
The following is an abbreviation example of traversing the array forEach:
function logArrayElements(element, index, array) { console.log("a[" + index + "] = " + element); } [2, 5, 9].forEach(logArrayElements); // logs: // a[0] = 2 // a[1] = 5 // a[2] = 9
Before the function starts, declare the variables It's a good practice to assign values. When declaring multiple variables:
let x; let y; let z = 3;
can be abbreviated as:
let x, y, z=3;
When using if to make basic judgments, the assignment operator can be omitted.
if (likeJavaScript === true)
The abbreviation is:
if (likeJavaScript)
You can use scientific notation to replace larger data, for example, 10000000 can be abbreviated as 1e7.
for (let i = 0; i <p> The abbreviation is: </p><pre class="brush:php;toolbar:false">for (let i = 0; i <h2>6. Multi-line string </h2><p> If you need to write a multi-line string in the code, just like the following: </p><pre class="brush:php;toolbar:false">const lorem = 'Lorem ipsum dolor sit amet, consectetur\n\t' + 'adipisicing elit, sed do eiusmod tempor incididunt\n\t' + 'ut labore et dolore magna aliqua. Ut enim ad minim\n\t' + 'veniam, quis nostrud exercitation ullamco laboris\n\t' + 'nisi ut aliquip ex ea commodo consequat. Duis aute\n\t' + 'irure dolor in reprehenderit in voluptate velit esse.\n\t'
But there is a simpler way, just use quotation marks:
const lorem = `Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse.`
The above is a complete introduction to the abbreviation skills that JavaScript developers need to know (primary part), if you want to know more about JavaScript video tutorial, please pay attention to the PHP Chinese website.
The above is the detailed content of Abbreviation skills that JavaScript developers need to know (primary). For more information, please follow other related articles on the PHP Chinese website!