Share practical experience with JavaScript newcomers

Source: Internet
Author: User
Tags eval

Do a little bit of performance improvement for JavaScript!

This article lists 24 suggestions to make your code writing process easier and more efficient. Maybe you're a JavaScript beginner, just finishing up your Hello world, and there's a lot of tips that will be useful to your work; maybe some of the tips you already know, try a quick glance and see if you can find something new!

Note: The Firebug console object is used many times in this article, please refer to the Firebug console API. For a more detailed description of Firebug, please bash here.

1. There are two different equality operators in place of = = = In lieu of ==javascript: ===|! = = and ==|! = By contrast, the former is more worthy of recommendation. Please try to use the former.

"If two comparison objects have the same type and value, = = = Return true,!== returns FALSE." ”

–javascript:the Good Parts

However, if you use = = and!=, you may encounter some unexpected problems when manipulating different data types. JavaScript attempts to convert them to a string, number, or Boolean before making equality judgments.

2. Avoid using the Eval function

The Eval function takes a string as a parameter and executes the string as a JavaScript statement, returning the result (reference).

Not only does this function reduce the execution efficiency of your script, but it also greatly increases the security risk because it gives too much power to the arguments as text. Don't use it!

3. Do not use the quick wording

Technically, you can omit most of the curly braces and the end of the semicolon, and most browsers will correctly execute the following statement:

.if(someVariableExists)
   x = false

However, if this is the case:

.if(someVariableExists)
   x = false
   anotherFunctionCall();

You might think it's the same as the following statement:

if(someVariableExists) {
   x = false;
   anotherFunctionCall();
}

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

As you can see, the beautiful indentation is no substitute for this gorgeous curly bracket. In all cases, write clearly the curly braces and the end of the sentence semicolon. It can be omitted occasionally when there is only one line of statements, although it is highly deprecated to do so:

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

Think more about the future, son.

Suppose you need to add more commands to this if statement in the future development process? And you're not going to have to add the parentheses?

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.