Character Set
Case Sensitivity
Whitespace
Comments
Literals
Identifiers
Reserved words
Optional semicolons
the lexical structure of a programming language
is
the set of elementary rules
that specifies how you write programs in that language
Character
set
Unicode
which is a computing industry standard for the consistent
encoding, representation and handling of text
expressed in most of the world's writing systems
JavaScript
uses UTF16 code units internally
externally, the browser determines the encoding
to support older technology
that can not display or input the full set of Unicode characters,
unicode escapes have been defined
\u followed by four hexadecimal digits
\u000A
// => UNICODE CHARACTER 'LINE FEED (LF)' (U+000A)
\u00E2
// => LATIN SMALL LETTER A WITH CIRCUMFLEX (U+00E2)
Case
sensitivity
capitalization of letters matters
keywords / variables / function names
&
all other identifiers must always be typed
with a consistent capitalization
the for keyword for example
must be always typed with small caps
NOT
For / FOR
<button onClick="someFunction()">Click Me</button>
Whitespace
spaces are ignored
line breaks for most part are ignored too
source code can be formatted
&
indented freely
Comments
JavaScript supports two styles of comments
block
/* ... */
&
single-line comments
// ...
var str = "JavaScript";
/*
var regex = /[A-Z]*/; str.replace(regex, '_');
*/
Literals
literals are fixed values NOT variables
that are literally provided in a script
JavaScript
has
numeric / string / boolean
object / array /
function / regular expression
literals
Numeric Literals
JavaScript has a single number type
10 //=> 10 - number ten
1.5 //=> 1.5 - number one point five
.2 //=> 0.2 - number zero point two
1e2 //=> 100 - number one hundred
-1 //=> -1 - number minus one
020 //=> 16 - number sixteen in base 8 (octal)
0x10 //=> 16 - number sixteen in base 16 (hexadecimal (hex))
[0xff, 0x00, 0x00] //=> [255, 0, 0] - it looks like?
parseInt('JavaScript'); //=> NaN
typeof NaN //=> "number"
NaN === NaN //=> false
1.79769313486231570e+309 //=> Infinity
String Literals
'JavaScript' //=> "JavaScript"
"JavaScript" //=> "JavaScript"
'' //=> ""
"" //=> ""
"\u005C" //=> "\"
Boolean Literals
represented by two literal values
true & false
Object Literals
represented by a list of zero or more pairs of
property names & associated values
enclosed in curly brackets
var course = {
name: 'Javasript - The Basics'
};
console.log(course);
//=> Object {name: "Javasript - The Basics"}
Array Literals
var course = ['JavaSript', 'The Basics'];
console.log(course); //=> ["JavaSript", "The Basics"]
A function literal defines a function value
formed by
function keyword / optional name
list of parameters / function body
var name = function (param1, param2, param3) { (function () {
console.log(param1, param2, param3);
})(); }
name('JavaScript', '-', 'The Basics'); //=> JavaScript - The Basics
provide compilation of the
regular expression when the script is loaded
var regex = /[A-Za-z0-9]/g;
Identifiers
used to
name variables & functions
or
to provide
labels for certain loops
identifiers must begin with
a letter
an underscore _
or a dollar sign $followed by
letters / digits / underscores / dollar signs
digits are not allowed as the first character
use only ASCII letters
are keywords
reserved by the language
&
cannot be used as identifiers
avoid using predefined global variables
&
function names
arguments / Array / Boolean / Date
decodeURI / decodeURIComponent / encodeURI / encodeURIComponent
Error / eval / EvalError / Function / Infinity / isFinite / isNaN / JSON
NaN / Number / Object / parseFloat / parseInt / RangeError / ReferenceError
RegExp / String / SyntaxError / TypeError / undefined / Math
have in mind that JavaScript implementations
may define other global variables & functions
the
semicolon ; is used to
separate
statements
to make the code clearer
it CAN usually be omitted
between two statements
if those statements are written on separate lines
at the end of a program
before a closing curly bracket }
JavaScript treats a line break as a semicolon if the
next non space character cannot be interpreted as a continuation
of the current statement
var last = 'JavaScript'
var first = 'The Basics'
var name = last + first
(last + first).toUpperCase()
//=> TypeError: string is not a function
if a statement begins with
( [ / + -
there is a chance that it could be interpreted
as a continuation of the previous statement
there are two exceptions
return true;
// is interpreted as: return; true;
// even if you meant:
return true;
i
++
j
// is parsed as:
i; ++j;
// not as:
i++; j;