I Know it well:
I read it in the grammar long ago.
-william Shakespeare, The tragedy (tragedy; disaster; tragedy) of Titus Andronicus
This chapter introduces the grammar of the good parts of JavaScript, presenting a quick overview of what the language is St Ructured. We'll represent the grammar with railroad diagrams.
The rules for interpreting these diagrams is simple:
you start on the left edge and follow the tracks to the right edge.
as you go, you'll encounter literals in ovals, and the rules or descriptions in rectangles.
any sequence that can being made by following the tracks is legal.
any sequence that cannot was made by following the tracks was not legal.
Railroad diagrams with one bar at each end allow whitespace to be inserted between any pair of tokens. Railroad diagrams with the bars at each end does not.
The grammar of the good parts presented in this chapter is significantly simpler than the grammar of the whole language.
Whitespace
Whitespace can take the form of formatting characters or comments. Whitespace is usually insignificant, but it's occasionally necessary to use whitespace to separate sequences of character s that would otherwise is combined into a single token. For example, in:
var this;
The space between Var and that cannot is removed, but the other spaces can be removed.
How to see:
The red circle below shows the way to go.
There are just a few types of things that you can walk, others are not allowed.
JavaScript offers the comments, block comments formed with/* */and line-ending comments starting with//. Comments should is used liberally to improve the readability of your programs. Take care, the comments always accurately describe the code. Obsolete (obsolete; old-fashioned) comments is worse than no comments.
The/* * * form of block comments came from a language called pl/i. Pl/i chose those strange pairs as the symbols for Comme NTS because they were unlikely to occur in this language ' s programs, except perhaps in string literals. In JavaScript, those pairs can also occur in regular expression literals, so block comments is not safe for commenting OU T blocks of code. For example:
/* var rm_a =/a*/.match (s); */
Causes a syntax error. So, it's recommended that/*/comments be avoided and//comments be used instead. In this book,//'ll be used exclusively.
Names
A name is a letter optionally followed by one or more letters, digits, or underbars. A name cannot be one of these reserved words:
AbstractBoolean Break byte Case Catch CharClass constContinueDebugger default Delete Do DoubleElseEnum Export extendsfalseFinalfinally float for functionGotoifImplements Importinch instanceof intInterfaceLongnativeNew NULLPackage Private protected publicreturn ShortStatic SuperSwitchsynchronized This ThrowThrows transienttrue Try typeofvarVolatilevoid while with
Most of the reserved words in this list is not used in the language. The list does not include some words that should has been reserved but were not, such as Undefined,nan, and Infinity. It is not permitted to name a variable or parameter with a reserved word. Worse, it is not permitted for use a reserved word as the name of the object property in an object literal or following a do T in a refinement.
Names is used for statements, variables, parameters, property Names, Operators,and labels.
Numbers
JavaScript has a single number type. internally, it is represented as 64-bit floating point, the same as Java ' s double. Unlike most other programming languages, there is no separate integer type, so 1 and 1.0 are the same value. This was a significant convenience because problems of overflow in short integers be completely avoided, and all you need To know on a number is the it is a number. A Large class of numeric type errors is avoided.
If A number literal have an exponent part and then the value of the literal is computed by multiplying the part before the E b Y raised to the power of the part after the e.so and 1e2 is the same number.
Negative numbers can be formed by using the–prefix operator.
The value NaN is a number value, which is the result of an operation, that cannot produce a normal result. NaN is not equal to any value, including itself. You can detect NaN with the IsNaN (number) function.
The value Infinity represents all values greater than 1.79769313486231570e+308.
Numbers has methods (see Chapter 8). JavaScript has a Math object, contains a set of methods that act on numbers. For example, the Math.floor (number) method can is used to convert a number into an integer.
Strings
A string literal can be wrapped in single quotes or double quotes. It can contain zero or more characters. The \ (backslash) is the escape character. JavaScript is built at a time when Unicode is a 16-bit character set, so all characters in JavaScript is bits W IDE.
JavaScript does not has a character type. To represent a character, make a string with just one character in it.
The escape sequences allow for inserting characters to strings that is not normally permitted, such as backslashes, quo TES, and control characters. The \u convention allows for specifying character code points numerically.
"A" = = = "\u0041"
Strings has a length property. For example, "seven". Length is 5.
Strings is immutable. Once It is made, a string can never be changed. But it's easy-to-make a new string by concatenating and strings together with the + operator.
Strings containing exactly the same characters in the same order is considered to be the same string. So:
' C ' + ' a ' + ' t ' = = = ' Cat '
Is true.
Strings has methods (see Chapter 8):
' Cat '. toUpperCase () = = = ' Cat '
Statements
Javascript-the Good Parts CHAPTER 2