Summary of JavaScript shorthand tips

Source: Internet
Author: User

In daily work, javascript some common shorthand skills, will directly affect our development efficiency, now the common skills are organized as follows:

1. Null (null, undefined) validation

When we create a new variable, we usually verify that the value of the variable is empty (null) or undefined (undefined). This is an often-considered validation for JavaScript programming.

Non-optimized code:

if NULL ") {Let variable2 = variable1;}

Optimized code:

In the Google Explorer developer panel under the console:

2. Arrays

Non-optimized code:

New Array (); a[0"myString1"; a[1"myString2"; a[2"myString3";

Optimized code:

Let a = ["MyString1", "MyString2", "MyString3"];

3. If true: Else optimization

Non-optimized code:

Let big; if Ten  true;} Else  false;}

Optimized code:

Let big = x > 10? True:false;

In the Google Explorer developer panel under the console:

4. Variable declaration

Non-optimized code:

3;

Optimized code:

Let x, Y, z=3;

5. Simplification of assignment statements

Non-optimized code:

x=x+11; y=y*;

Optimized code:

X++;minuscount--;y*=10;

Assuming x=10,y=5, then the basic arithmetic operation can be done using the following shorthand method:

In the Google Explorer developer panel under the console:

6. Avoid using RegExp objects

Non-optimized code:

var New REGEXP ("\d+ (.) +\d+","igm"= re.exec ("padding 01234 text text 56789 padding"//" 01234 text text 56789 "

Optimized code:

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

7. If condition Optimization

Non-optimized code:

if true)

Optimized code:  

if (likejavascript)

8. Alternatives to CharAt ()

Non-optimized code:

" myString ". CharAt (0);

Optimized code:

"MyString" [0]; Return ' m '

Summary of JavaScript shorthand tips

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.