24 tips for beginners of JavaScript (1)

Source: Internet
Author: User

A little performance improvement for JavaScript!

This article lists 24 suggestions that make your code writing process easier and more efficient. Maybe you are still a beginner in JavaScript and have just finished writing your Hello World. There are many tips that will be very useful for your work. Maybe you know some tips, let's take a quick look and see if we can find something new!

1. Replace = with =

There are two different equal operators in JavaScript: ===|! ==And ==|! =. In contrast, the former is more recommendable. Use the former as much as possible.

"If two comparison objects share the same type and value, = true is returned ,! = False is returned ."
-JavaScript: The Good Parts

However, if the = and! =, You may encounter unexpected problems when operating on different data types. Before equality determination, JavaScript tries to convert them into strings, numbers, or Boolean values.

2. Avoid using Eval Functions

The Eval function uses a string as a parameter and executes the string as a JavaScript statement to return the result.

This function not only reduces the execution efficiency of your script, but also greatly increases the security risk, because it gives too much rights as text parameters. Never use it!

3. Do not use Quick writing

Technically, you can omit most of the curly arc and semi-colon. Most browsers can correctly execute the following statements:

 
 
  1. if(someVariableExists)   
  2. x = false 

However, if so:

 
 
  1. if(someVariableExists)   
  2. x = false 
  3. anotherFunctionCall(); 

You may think it is equal to the following statement:

 
 
  1. if(someVariableExists) {   
  2. x = false;   
  3. anotherFunctionCall();  
  4. }

Unfortunately, this is not the case. The reality is that it is equivalent:

 
 
  1. if(someVariableExists) {   
  2. x = false;   
  3. }
  4. anotherFunctionCall(); 

As you noted, the beautiful indentation cannot replace this gorgeous curly arc. In all cases, please clearly write curly braces and semicolons. It can be omitted occasionally when there is only one line of statements, although this is not recommended below:

 
 
  1. if(2 + 2 === 4) return 'nicely done'; 

Think about the future, child.

Suppose that you need to add more commands for this if statement in the future development process? Why don't you add brackets?

4. Make good use of JS Lint

JSLint is a debugger written by Douglas Crockford. You only need to paste your code to quickly scan for any obvious errors and problems.

"JSLint scans the received code. Locate the problem, describe the problem, and give the approximate location in the source code. Possible problems include but are not limited to syntax errors, although syntax errors are indeed the most common. JSLint also checks the formatting style and structure errors of the Code with conventions. Scanning through JSLint does not guarantee that your program is completely correct. It just provides you with an extra pair of eyes for error detection ."
-JSLint document

Before you complete the code, put it in JSLint and check it to quickly eliminate your unintentional experience.


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.