Easy Learning javascript--Part 1th: Understanding let statements

Source: Internet
Author: User
Tags jquery library

Using the Let statement allows you to create block-scoped local variables in JavaScript. The Let statement was introduced in the JavaScript ECMAScript 6 standard.

Before you get down to the Let statement, I recommend that you review the Ignite UI based on the Infragistics jquery library, which can help you write and run your Web application faster. You can use the Ignite UI of the JavaScript library to quickly resolve complex lob requirements in html5,jquery,angular,react or ASP. (You can download the free trial version of the Ignite UI here.) )

Before ECMAScript 6, JavaScript had three types of scopes:

    • Global scope
    • Function range
    • Vocabulary Range

To explore the Let statement in detail, consider the following code snippet:

Foo () {    console.log (console.log ("Value of x outside if statement =" + x);} Foo ();   

The above code gets the output:

In the above code, we declare the variable x using the var statement. Therefore, the range of the variable x is the function range. The variable x inside the IF statement is the variable x created outside of the IF statement. Therefore, when you modify the value of the variable x in the IF statement block, the value of all references to the variable x in the function is also modified.

To avoid this, you need to use block-level scopes, which allow you to create block-scoped local variables.

Modify the code snippet above and declare the variable with a let statement:

Foo () {    console.log (console.log ("Value of x outside if statement =" + x);} Foo ();   

In the preceding code snippet, we use the LET statement to declare the scope-level local variable x. Therefore, updating the value of the variable x within the IF statement does not affect the value of the variable x outside of the IF statement.

The following is the output of the above code:

Unlike variables declared with a function scope (or global scope), variables declared with let are block-scoped: They exist only in the blocks they define.

Variable Promotion

Using a let declaration for a variable promotion is different from a variable declared with Var. Therefore, variables declared with Let do not have a variable promotion, which means that variables declared with let are not moved to the top of the execution context.

To understand this better, take a look at the following code:

Foo () {    console.log (x);    67;} Foo ();  

As output, you get the referenceerror of the variable y, and the variable y is declared with a let statement. Variables declared with let are not promoted to the execution context.

Re-declaring variables

You cannot use let to re-declare a variable in the same function or block. There is a syntax error in doing so. Take a look at the following code:

Foo () {    if (89;}} Foo ();  

Running the above code will come up with a syntax error, as follows:

Temporary Dead Zone

Sometimes a variable that is declared with a let causes a temporary dead zone. In the following code, let x=x+67 throws an X-undefined exception.

This error occurs because the expression (x + 67) is the value of the local variable x within the scope of the If block, not the value of the local variable x within the scope of the function. Run the above code and you will get an exception like this:

You can fix the above error by moving the declaration variable to the upper line of the expression, as follows:

Block-level scoping is one of the most important features of any programming language, and with the introduction of let statements in ECMAScript 6, JavaScript now has this capability. Using the Let statement allows you to create a scoped variable within a scope. This can solve many problems, such as accidental modification of global scope variables, Local variables in closures, and help in writing clearer code.

In the next article in the easy to learn JavaScript series, we'll cover the rest parameters in JavaScript functions. You can learn more about this in the ECMA International website. Also, don't forget to view the Ignite UI, which can be combined with html5,angular,react or ASP. NET MVC to help create rich Internet applications.

http://cqjlckj.com/
http://zgrwszjy.com/
http://xinglongwaji.com/
http://lyyrmm.com/
http://xmmczs.com/
http://sdcranes.com/
http://xyhysp.com/
http://dmxxj.com/
http://tjcsd.net/
http://hnrxz.com/
http://7doing.com/
http://cjopto.com/
http://huaaoedu.cn/
http://gdgyym.com/
http://whzjsy.com/
http://casamalheiro.com/
http://casashawaii.com/
http://gdshuaikang.com/
http://hxdr2008.com/

Easy Learning javascript--Part 1th: Understanding let statements

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.