function scope and block-level scope--JavaScript you don't know

Source: Internet
Author: User

The ET and const declarations within {} will become externally inaccessible values, but Const declares constants and cannot modify

The function is the most common scope unit in JavaScript. In essence, a variable or function declared inside a function will
In the scope of the "hidden" up, this is intentional good software design principles.
But the function is not the only scope unit. Block scopes refer to variables and functions that not only belong to the scope,
It can also belong to a block of code (usually referred to as {:} internal).
Starting with ES3, the Try/catch structure has a block scope in the catch clause.
The Let keyword (a cousin of the Var keyword) was introduced in ES6 to declare variables in any block of code. If
(..) {Let A = 2;} Will declare a variable with the IF {.} block, and add the variable to the block
In
Some people think that block scopes should not be used entirely as an alternative to function scopes. Both functions should be present at the same time, open
The sender can and should also choose which scopes to use as needed to create good code that is readable and maintainable.

function scope and block-level scope--JavaScript you don't know

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.