JavaScript like many languages (eg C, Java, awk, perl) is based on a common syntax and grammar developed by the Bell Labs in the 60's. This makes it easy to cross over from one language to another based on program requirements, resources and politics. This tutorial on JavaScript syntax and grammar (ie. programming in the small) assumes introductory programming experience in another language and includes the following topics:
Lexical Structure
The lexical structure of a programming language is the set of elementary rules that define what are the tokens or basic atoms of the program. It is the lowest level syntax of a language and specifies what is punctuation, reserved words, identifiers, constants and operators. Some of the basic rules for JavaScript are:
JavaScript is case sensitive.
Whitespace, tabs, and newline characters are ignored except when part of string constants. They can be added as needed for readability.
Single line comments begin with //
Multiline comments begin with /* and end with */
Statements terminate in semicolons! Make sure to always terminate statements with a semicolon.
Commas are used to separate words in a list
Round brackets are used for operator precedence and argument lists.
Square brackets are used for arrays and square bracket notation.
Curly or brace brackets are used for blocks.
Keywords are reserved words that have special meanings within the language syntax.
Identifiers are names for constants, variables, functions, loop labels, objects and classes. The first character must be an ASCII letter, underscore or dollar sign. Following characters can also include digits. JavaScript style begin class identifiers with a capital letter, uppercase constant ids and lowercase variables.Note: An identifier must NOT be any word on the JavaScript Reserved Word List.
Literal Constants
Literal constants are values that do not change within a program. Some types of literal constants are:
Boolean: true, false
Numeric 5, 2.543, 8e12, -4.1E-6, 073 [leading zero] (octal), 0xFF (hexadecimal)
String: 'fred', "Fred and Ethel"
Primitive: Infinity, NaN, null, undefined
Warning: Do not use leading zeros to format integers as this may give an unintended octal meaning. Use spaces instead!
Note: Floating point numbers less than one should begin with a leading zero. For example write 0.1 rather than .1
Warning: If a number begins with two zeros and contains a decimal point (eg. 005.3) it will generate an error. Validation techniques to guard against this type of error will be discussed in Getting the Bugs Out.
One question that is often asked is 'WHY can one use either kind of quote marks (they must match however) when defining a STRING value?'. Occasionally there is a need to have a string that uses an internal quote or possessive form or contraction (apostrophe). To handle one of these situations, bracket the string with the opposite form of quote.
Lexical Structure
The lexical structure of a programming language is the set of elementary rules that define what are the tokens or basic atoms of the program. It is the lowest level syntax of a language and specifies what is punctuation, reserved words, identifiers, constants and operators. Some of the basic rules for JavaScript are:
JavaScript is case sensitive.
Whitespace, tabs, and newline characters are ignored except when part of string constants. They can be added as needed for readability.
Single line comments begin with //
Multiline comments begin with /* and end with */
Statements terminate in semicolons! Make sure to always terminate statements with a semicolon.
Commas are used to separate words in a list
Round brackets are used for operator precedence and argument lists.
Square brackets are used for arrays and square bracket notation.
Curly or brace brackets are used for blocks.
Keywords are reserved words that have special meanings within the language syntax.
Identifiers are names for constants, variables, functions, loop labels, objects and classes. The first character must be an ASCII letter, underscore or dollar sign. Following characters can also include digits. JavaScript style begin class identifiers with a capital letter, uppercase constant ids and lowercase variables.Note: An identifier must NOT be any word on the JavaScript Reserved Word List.
Literal Constants
Literal constants are values that do not change within a program. Some types of literal constants are:
Boolean: true, false
Numeric 5, 2.543, 8e12, -4.1E-6, 073 [leading zero] (octal), 0xFF (hexadecimal)
String: 'fred', "Fred and Ethel"
Primitive: Infinity, NaN, null, undefined
Warning: Do not use leading zeros to format integers as this may give an unintended octal meaning. Use spaces instead!
Note: Floating point numbers less than one should begin with a leading zero. For example write 0.1 rather than .1
Warning: If a number begins with two zeros and contains a decimal point (eg. 005.3) it will generate an error. Validation techniques to guard against this type of error will be discussed in Getting the Bugs Out.
One question that is often asked is 'WHY can one use either kind of quote marks (they must match however) when defining a STRING value?'. Occasionally there is a need to have a string that uses an internal quote or possessive form or contraction (apostrophe). To handle one of these situations, bracket the string with the opposite form of quote.
No comments:
Post a Comment