Part 1 Core JavaScript
Chapter 2. Lexical Structure
2.1 Character Set
JavaScript programs are written using the Unicode character set.
2.2 Case Sensitivity
2.3. Whitespace and Line Breaks
JavaScript ignores spaces, tabs, and newlines that appear between tokens in programs
2.4. Optional Semicolons
2.5. Comments
//comments and /*comments*/
2.6. Literals
2.7. Identifiers
The first character must be a letter, an underscore (_), or a dollar sign ($).
2.8 Reserved Words
Table 2-1. Reserved JavaScript keywords
break | do | if | switch | typeof |
case | else | in | this | var |
catch | false | instanceof | tHRow | void |
continue | finally | new | true | while |
default | for | null | try | with |
delete | function | return |
|
Table 2-2. Words reserved for ECMA extensions
abstract | double | goto | native | static |
boolean | enum | implements | package | super |
byte | export | import | private | synchronized |
char | extends | int | protected | throws |
class | final | interface | public | TRansient |
const | float | long | short | volatile |
debugger |
|
|
|
as, is, namespace, and use
Chapter 3. Datatypes and Values
3.1. Numbers
3.1.1. Integer Literals
represent all integers between -9007199254740992 (-253) and 9007199254740992 (253)
3.1.2. Hexadecimal and Octal Literals
0x 0X 0
3.1.3. Floating-Point Literals
[digits][.digits][(E|e)[(+|-)]digits]
Special numeric constants
Constant | Meaning |
---|---|
Infinity | Special value to represent infinity |
NaN | Special not-a-number value |
Number.MAX_VALUE | Largest representable number |
Number.MIN_VALUE | Smallest (closest to zero) representable number |
Number.NaN | Special not-a-number value |
Number.POSITIVE_INFINITY | Special value to represent infinity |
Number.NEGATIVE_INFINITY | Special value to represent negative infinity |
3.2. Strings
3.2.2. Escape Sequences in String Literals
Table 3-2. JavaScript escape sequences
Sequence | Character represented |
---|---|
\0 | The NUL character (\u0000). |
\b | Backspace (\u0008). |
\t | Horizontal tab (\u0009). |
\n | Newline (\u000A). |
\v | Vertical tab (\u000B). |
\f | Form feed (\u000C). |
\r | Carriage return (\u000D). |
\" | Double quote (\u0022). |
\' | Apostrophe or single quote (\u0027). |
\\ | Backslash (\u005C). |
\xXX | The Latin-1 character specified by the two hexadecimal digits XX. |
\uXXXX | The Unicode character specified by the four hexadecimal digits XXXX. |
\XXX | The Latin-1 character specified by the octal digits XXX, between 1 and 377. Not supported by ECMAScript v3; do not use this escape sequence. |
3.2.4. Converting Numbers to Strings
String(number)
Number.toString(number)
toFixed
toExponential
toPrecision
var n = 123456.789;
n.toFixed(0); // "123457"
n.toFixed(2); // "123456.79"
n.toExponential(1); // "1.2e+5"
n.toExponential(3); // "1.235e+5"
n.toPrecision(4); // "1.235e+5"
n.toPrecision(7); // "123456.8"
3.2.5. Converting Strings to Numbers
When a string is used in a numeric context, it is automatically converted to a number.
by simply subtracting zero from it
Number(string_value);
parseInt("ff", 16); //255
parseFloat('string')
3.3. Boolean Values
true --> 1
false --> 0
true --> 'true'
false --> 'false'
number --> true
0,NaN --> false
'' --> false
'string' --> true
null, undefined --> false
Boolean(arg);
3.4. Functions
function square(x) { return x*x; }
var square = function(x) { return x*x; }
var square = new Function("x", "return x*x;");
3.5. Objects
3.5.1. Creating Objects
var o = new Object( );
var now = new Date( );
3.5.3. Object Conversions
3.6. Arrays
3.6.1. Creating Arrays
var a = new Array( );
a[0] = 1.2;
a[1] = "JavaScript";
a[2] = true;
a[3] = { x:1, y:3 };
var a = new Array(1.2, "JavaScript", true, { x:1, y:3 });
var a = new Array(10);
var a = [1.2, "JavaScript", true, { x:1, y:3 }];
3.5.2. Object Literals
3.7. null
null is a special value that indicates no value
3.8. undefined
undefined is returned when you use either a variable that has been declared but never had a value assigned to it or an object property that does not exist. Note that this special undefined value is not the same as null.
3.9. The Date Object
3.12. Type Conversion Summary
Table 3-3. Automatic datatype conversions
Value
Context in which value is used
String
Number
Boolean
Object
Undefined value
"undefined"
NaN
false
Error
null
"null"
0
false
Error
Nonempty string
As is
Numeric value of string or NaN
TRue
String object
Empty string
As is
0
false
String object
0
"0"
As is
false
Number object
NaN
"NaN"
As is
false
Number object
Infinity
"Infinity"
As is
true
Number object
Negative infinity
"-Infinity"
As is
TRue
Number object
Any other number
String value of number
As is
true
Number object
true
"true"
1
As is
Boolean object
false
"false"
0
As is
Boolean object
Object
toString( )
valueOf( ), toString( ), or NaN
true
As is
covers advanced material
3.13. Primitive Datatype Wrapper Objects
3.14. Object-to-Primitive Conversion
3.15. By Value Versus by Reference