Home > Web Front-end > JS Tutorial > Abbreviation skills that JavaScript developers need to know (primary)

Abbreviation skills that JavaScript developers need to know (primary)

云罗郡主
Release: 2018-10-19 14:32:23
forward
1531 people have browsed it

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.

Abbreviation skills that JavaScript developers need to know (primary)

#1. Ternary operator

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';
}
Copy after login

The abbreviation is:

const answer = x > 10 ? 'greater than 10' : 'less than 10';
Copy after login

2. Loop statement

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)
Copy after login

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
Copy after login

3. Declare variables

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;
Copy after login

can be abbreviated as:

let x, y, z=3;
Copy after login

4. If statement

When using if to make basic judgments, the assignment operator can be omitted.

if (likeJavaScript === true)
Copy after login

The abbreviation is:

if (likeJavaScript)
Copy after login

5. Decimal number

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'
Copy after login

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.`
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template