Javascript Strict mode

Source: Internet
Author: User

Introduction

Strict mode is a way to introduce better error checking into your code. when you use strict mode, you cannot use implicitly declared variables, assign values to read-only properties, or add properties to objects that are not extensible.

Declaring strict mode

You can declare strict mode by adding "use strict" at the beginning of a file, program, or function. Such declarations are referred to as "instruction preamble". the scope of a strict mode declaration depends on its context. If strict mode is declared in the global context (outside the scope of the function), all code in the program is in strict mode. If you declare strict mode in a function, all the code in the function is in strict mode. For example, in the following example, all code is in strict mode, and a variable declaration outside the function causes a syntax error "undefined variable in strict mode".

[JavaScript]View Plaincopyprint?
    1. "Use strict";
    2. function TestFunction () {
    3. var testvar = 4;
    4. return TestVar;
    5. }
    6. There will be an error.
    7. TestVar = 5;

In the following example, only the code in TestFunction is in strict mode. a variable declaration outside a function does not cause a syntax error, but a declaration inside a function causes a syntax error.

[JavaScript]View Plaincopyprint?
    1. function TestFunction () {
    2. "Use strict";
    3. //There will be an error
    4. TestVar = 4;
    5. return TestVar;
    6. }
    7. TestVar = 5;


Javascript Strict mode

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.