|
|
JavaScript treats whitespace characters as separators but
otherwise ignores them. Additionally, newlines insert automatic semicolons when possible. In
either case, a liberal and consistent use of whitespace makes scripts
easier to read and more maintainable.
Code |
Name |
' ' 0x20
|
Space |
'\t' 0x09
|
Tab |
'\v' 0x0b
|
Vertical Tab |
'\f' 0x0c
|
Form Feed (new page) |
'\n' 0x0a
|
Newline |
'\r' 0x0d
|
Carriage Return |
Automatic semicolon insertion
|
Because newlines naturally separate statements, scripts can often omit the
semicolon separator. Scripts must be aware of the automatic
semicolons because in a few cases, statements may be unexpectedly
separated.
Semicolon insertion for returns
function foo()
{
return // semicolon inserted here
3 + 3 // this expression is _not_ the return value
}
|
Automatic semicolon insertion with functions
foo // semicolon inserted here, so foo is treated as a
(15) // variable, not a function call.
|
No automatic semicolon insertion in expression
foo( // no semicolon inserted here, so foo(15) is called
15)
|
No automatic semicolon insertion in for
for(var i = 0 // no semicolon inserted here, so this is an error
i < 3 // no semicolon inserted here, so this is an error
i++) {
}
|
The default source encoding is Latin-1, i.e. ISO-8859-1. Because
characters are 16-bits and the source file is 8-bits, the source file
is an encoding of the 16-bit characters. The default encoding
converts the bytes to the character with the same number, i.e. byte
245 becomes character 245. This poses a problem for languages like
Japanese which use all 16 bits.
The calling program, e.g. JSP, will set the
encoding using a program-specific method.
Setting the encoding in JSP
<%@ encoding=utf8 %>
<title>Hello, world</title>
<h1>Hello, world</h1>
|
Copyright © 1998-2000 Caucho Technology. All rights reserved.
Last modified: Thu, 16 Sep 1999 14:56:49 -0700 (PDT)
|