JS Basic Concept-Grammar

Source: Internet
Author: User

ECMAScript syntax draws heavily on C and other classes of C languages.

1. Case-sensitive

Everything in ECMAScript (variables, function names, and operators) is case-sensitive. This means that the variable name test and the variable name test represent two different variables, respectively.

2. Identifiers

The so-called identifier refers to the name of a variable, function, property, or parameter of a function. Identifiers can be one or more characters that are combined according to the following formatting rules:

    • The first character must be a letter, an underscore (_), or a dollar sign ($);
    • Other characters can be letters, underscores, dollar signs, or numbers.

By convention, the ECMAScript identifier is in hump-case format, that is, the first letter lowercase, and the first letter of each meaningful word is capitalized:

Firstsecond

MyCar

Dosomethingimport

3. Notes

ECMAScript uses C-style annotations, including single-line comments and block-level annotations.

Single-line Comment

/* Block-level comments */

4. Strict mode

ECMAScript5 introduced the concept of strict mode (strict modes). Strict mode defines a different parsing and execution pattern for JavaScript. In strict mode, some of the indeterminate behavior in ECMASCRIPT3 will be handled, and some unsafe operations will run out of error. To enable strict mode throughout the script, you can add the following code at the top:

"Use strict";

Use strict mode in the specified function:

function dosomething () {
"Use strict";
  //function Body }

5. Statements

The statement in ECMAScript ends with a semicolon, and if the semicolon is omitted, the parser determines the end of the statement.

var sum = a + b        // Even if no semicolon is a valid statement-- var diff =-A is not recommended;    // valid statements--recommended

6. Keywords and reserved words

Keywords can be used to represent the start or end of a control statement, or to perform a specific operation. The keyword cannot be used as an identifier. Marked with * is the fifth edition of the New keyword

 Break         Do            instanceof        typeof        Delete Case         Else          New               var           inchCatch        finally       return            void          defaultContinue      for           Switch             while         ifdebugger*     function       This               with          Throw Try

Reserved words that cannot be used as identifiers, although reserved words do not have any specific purpose in the language, they may be used as keywords in the future, and the following are all the reserved words defined by ECMA-262 Third edition:

Abstract     enum          intshort                    boolean       export        interface          Staticbyte        extends       long               Super Char         Final         native             synchronizedclass         floatpackage            throwsconst         Goto          Private            transientdebugger      implements    protected          volatile  Double        import        Public

7. Variables

The ECMAScript variables are loosely typed, and so-called loose types can be used to hold any type of data. In other words, each variable is just a placeholder for holding the value. Use the var operator when defining variables.

varMessage1;//defines a variable named message that can be used to hold any valuealert (MESSAGE1);//undefined uninitialized variables will save a special value undefined/*ECMAScript also supports direct initialization of variables. * The variable message holds a string value "HI", and the process of initializing it is assigning a value to the variable. */varMessage2 = "Hi"; Message3= 100;//valid but not recommended/*A variable defined with the var operator becomes a local variable defined in the scope of the variable * that is, if the function uses var to define a variable, then the variable will be destroyed after the function exits*/functionTest () {varMessage4 = "HI";//Local Variables}test (); alert (message4); //uncaught referenceerror:message4 is not defined                                        functiontest2 () {message5= "HI";//Global variables, omitting the var operator}test2 (); alert (MESSAGE5); //Hi

JS Basic Concept-Grammar

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.