Chapter
2 : Javascript Variables
Executing
JavaScripts
Javascript execution
begins after the HTML documents loads into the browser, but before the user can
interact with the document .The browser reads in all javascript statement ,as
it does HTML statements ,and then begins interpreting the Javascript code.
If your
Javascript scripts are stored in a
separate file, they are also evaluated when the page loads and before any
script action take place.
All Javascript
statements that are contained within a function block are interpreted , and
execution does not occur until the function is called from a Javascript
event.Javascript statements that are not within a function block are executed
after the document loads into the browser.The execution results of the latter
will be apparent to the users when they first view the page. The second process
which Javascript executes statements is though function calls . Any statement
contained within a function will not be executed until a javascript event calls
the function.
Writing
the script
As in any other
programming language , Javascript statements can be implemented using various
methodologies. Functions has to be defined in the <head> section and then
calling these functions within the HTML body is the best way to take advantage
of the object-based Javascript language. Javascript is simple to understand.
Although HTML statements are not case-sensitive , javascript statements are.
When beginning
to write your code , keep following items in mind:
F code reuse
F Readability
F Ease of modification
You can use
Javascript tags in either the body or the head of a document.As recommended
previously , placing the <script> in the head rather than the body
ensures that all statements will be evaluated (and executed , if necessary)
before the user interacts with the document.
The hazards of
putting script statements in the body of the document are varied.Depending on
the specific tags and the order of the document , you can never be positive
that the user will not interact with the script in the wrong manner or react to
the page before the script has fully loaded or executed.If any of these
occur,the effect that you want for your page might not be seen.(After all your
effort ,who wants that?) The practice of defining your javascript functions and
then calling them from the body will ensure that all the functions are
evaluated before the user can begin interaction with the page.
Running
the script
As you probably
realized , javascript scripts are as simple to load as HTML documents.you do
not have to explicity execute any code to run your scripts ; because you place
your code in the HTML document or call it explicity in the first script line,
your script will run when the page loads. Remember that not all the code will
necessarily execute immediately upon loading. Code that is enclosed in a
function call is only evaluated when page loads but does not execute in a
function call runs after the paqe finishes loading but before the user has a
chance to interact with the page.
Tokens
Tokens are the
smallest individual words, phrases, or characters that Javascript can
understand. When Javascript is interpreted , the browser parses the script into
these tokens while ignoring comments and white space.
Javascript
tokens fit in five categories:
Ø identifiers
Ø keywords
Ø literals
Ø operators
Ø separators
Identifiers
Identifiers are
simply names that represent variables, methods, or objects. They consist of a
combination of a combination of characters and digits. some names are already
built into the Javascript language and are therefore reserved these identifiers
are called as "keyword" . Aside from
these keywords , you can define your own creative and meaningful identifiers.
Ofcourse , you have a couple of rules to follow. You must begin all identifiers
with either a letter or underscore(_). You can then use letters , digits, or
underscores for all subsequent characters. Letters include all uppercase
characters, A through Z, and all lowercase Characters, a through z. Digits
include the characters 0 through 9.The table below shows some examples of valid
and invalid identifiers.
|
valid |
Invalid |
Reason |
|
current_WebSite |
current
WebSite |
contains a
space |
|
NumberOfHits |
#ofIslands |
pound sign is
prefixed |
|
n |
2bOrNotToBe |
begins with a
number |
|
N |
Return |
Keyword |
Keywords
keywords are
predefined identifiers that make up the core of a programming language. In
Javascript , they perform unique functions such as declaring new variables and
functions, making decisions based on the present state of the computer, or
starting a repetitive loop inside your application. Keywords , which are built
into Javascript , are always available for use by the programmer but most
follow the correct syntax.
|
break |
if |
this |
|
continue |
in |
true |
|
else |
int |
var |
|
false |
new |
while |
|
for |
null |
with |
|
function |
return |
|
Reserved
Words
Reserved words
are identifiers that you might not use as names for javascript variables ,
functions, objects , or methods. This includes keywords along with identifiers
that are set aside for possible future use.
Literals
Literals are
data comprised of numbers or strings used to represent fixed values in
Javascript. They are values that do not
change during the execution of your scripts. The following five sections
contain descriptions and examples of the different types of literals that you
can use.
Integer
Literals
Integers can be
expressed in either decimal (base 10), octal (base 8), or hexadecimal (base 16)
format. An integer literal in decimal format can include any sequence of digits
that does not begins with a 0 (zero). A zero in front of an integer literal
designates octal form. The integer itself can includes a sequence of the digits
0 through 7, To designate hexadecimal , 0x (0X) is used before the
integer.Hexadecimal integers can include digits 0 through 9 along with the
letters a through for A through F. Some examples includes
|
Decimal(base
10) |
33,2139 |
|
Octal(base 8) |
071,03664 |
|
Hexadecimal(base
16) |
0x7b8, oX395 |
Floating
Point Literals
Floating-point
literals represents decimal numbers with fractional parts.They can be expressed
in either standard or scientific
notation. With scientific notation , use either e or E to designates the
exponent. Both the decimal number and exponent can be either signed or unsigned
as shown in the examples:
3405.673
-1.958
8.3200e+11
8.3200e11
Boolean
Literals
Javascript
implements Boolean data types and therefore supports the two literals , true
and false.
They represent
the Boolean values 1 and 0,respectively. The true and false keywords must
appear in lowercase. As a result , the capitalized words True or False are left
open to define as your own identifiers, but it is not recommended.
String
Literals
A string
literal is zero or more characters enclosed in double (“) or single (‘) quotes.
Javascript gives you this option, but you must use the same type of quote to
surround each string. The following are examples are examples of string
literals enclosed in quotes:
“virtual
communities”
“virtual
communities”
“Look , up in the sky”
Special
Characters
When writing
scripts, you might sometimes need to tell the computer to use a special
character or keystroke such as a tab or carriage return. To do this, use a
backslash in front of one of the special characters as shown in following list:
\b indicates a backspace
\f indicates a form feed
\n indicates a new-line character
\r indicates a carriage return
\t indicates a tab character
Variables
A variable is
the name given to a location in a
computer’s memory where data is stored.
The name of a
Javascript variable comprises one or more letters , digits or underscores but
cannot begin with a digit. Digits includes 0 through 9, Letters include all
uppercase characters, A through Z, and all lowercase characters , a through z.
Declaring
Variables
To let Javascript
know you are going to use an identifier as a variable , you must first declare
it.To declare variables in Javascript , use the keyword var followed by the new
variable name . The action reserves the name as a variable to be used as a
storage area for whatever data you might want to hold with it. In the examples
that follow , notice that you can also declare more than one variable at a time
by using a comma between variable names:
Var
internetAddress
Var n
Var I,j,k
Var
isMouseOverLink, helloMessage
Once a variable
is declared , it is then ready to be filled with its first value. This
initializing is done with the assignment operator , =. You can initialize a
variable at the same time it is declared or at any point thereafter in your
script. Assigning a value when the variable is declared or at any point
thereafter in your script. Assigning a
value when the variable is declared or at any point thereafter in your script.
Assigning a value when the variable is declared can help you remember what type
of value you originally meant the variable hold.
Variable
Type
When storing a
piece of data (more commonly known as a
value), Javascript data types.
Scope
of Variables
The scope of a
variable refers to the areas within a program where a variable can be
referenced.
Local
A variable
declared inside a function is local in scope. Only that function has access to
the value that the variable holds.Each time the function is called , the
variable is created. Likewise, each time the function ends, the variable is
destroyed. Another function can also declare a variable with the same name, but
javascript considers it a different variable and does not address the same
block of memory.
Global
If you want
more than one function to share a variable , you declare the variable outside
of any functions (but , of course ,inside the <script> tags.). with this
method, any part of your application, including all functions, can share one instance
of a variable. I recommend that you declare global.
Constants
Javascript does
not supply any built-in constants. You could call true and false constants, but
Netscape really categorizes them as keywords. A constant holds the same value
throughout an application so that you can be sure it always carries the same
value. Netscape might find a need for constants in the future , but for now ,
it works fine without them.

No comments:
Post a Comment