JavaScript Tutorial for Programmers - JavaScript Grammar
July 12, 1998
JavaScript code, much like other programming languages,
is made up of statements which serve to make assignments,
compare values, and execute other sections of code.
By and large, programmers will already be familiar with
JavaScript's usage of variables, operators, and statements.
Below is a chart summarizing the main elements of JavaScript
grammar. Following, we will look at each element in detail.
|
Variables
|
Labels which refer to a changeable value.
Example:
total may be possess a value of 100. |
|
Operators
|
Actors which can be used to calculate or
compare values.
Example: Two values may be summed using the addition
operator (+);
total+tax
Example: Two values may be compared using the greater-than
operator (>);
total>200 |
|
Expressions
|
Any combination of variables,
operators,
and statements which evaluate to some result.
In English parlance this
might be termed a "sentence"
or even a "phrase",
in that grammatical elements are combined
into a cogent meaning.
Example:
total=100;
Example:
if
(total>100)
|
|
Statements
|
As in English,
a statement pulls all grammatical elements together into a full
thought. JavaScript statements may take the form of conditionals,
loops, or object manipulations. It is good form to separate
statements by semicolons, although this is only mandatory if
multiple statements reside on the same line.
Example:
if
(total>100)
{statements;} else {statements;}
Example:
while
(clicks<10) {statements;} |
|
Objects
|
Containing constructs which possess a set of values,
each value reflected into an individual
property of
that object. Objects are a critical concept and feature of
JavaScript. A single object may contain many properties,
each property which acts like a variable reflecting a certain value.
JavaScript can reference a large number of "built-in"
objects which refer to characteristics of a Web document.
For instance, the document object contains properties which
reflect the background color of the current document,
its title, and many more. For a fuller explanation of the
built-in objects of JavaScript, see the section on
"Document Object Model". |
|
Functions and Methods
|
A JavaScript function is quite similar to a
"procedure" or "subroutine"
in other programming languages. A function is a discrete set
of statements which perform some action. It may accept incoming
values (parameters), and it may return an outgoing value.
A function is "called" from a JavaScript statement
to perform its duty. A method is simply a function which is
contained in an object. For instance, a function which
closes the current window, named close(),
is part of the window object; thus,
window.close() is known as a method. |
JavaScript Tutorial for Programmers - Embedding JavaScript
JavaScript Tutorial for Programmers
JavaScript Tutorial for Programmers - Variables and Data Types
|