Main Page

ECMAScript Basics

ECMAScript Basics
Some simple JavaScript functionality is easy to accomplish in the browser. Numerous articles on
the Internet show you how to accomplish what many term “stupid Web tricks” using JavaScript.
These tricks include how to pop up notices to the user, swap images, and create simple games.
Although these are all interesting pieces of functionality to add to Web sites, copying and pasting
code doesn’t provide an understanding of why or how something works. This chapter aims to
provide you with a deeper knowledge base about how JavaScript works by examining its core,
ECMAScript.
As described in the previous chapter, ECMAScript provides JavaScript with syntax, operators, and
basic objects necessary to complete common programming tasks.
Syntax
Developers familiar with languages such as Java, C, and Perl will find ECMAScript syntax easy to
pick up because it borrows syntax from each. Java and ECMAScript have several key syntax fea-
tures in common, as well as some that are completely different.
The basic concepts of ECMAScript are the following:
?
Everything is case-sensitive.
Just as with Java, variables, function names, operators, and
everything else is case-sensitive, meaning that a variable named
test
is different from
one named
Test
.
?
Variables are loosely typed.
Unlike Java and C, variables in ECMAScript are not given a
specific type. Instead, each variable is defined using the
var
operator and can be initial-
ized with any value. This enables you to change the type of data a variable contains at any
point in time (although you should avoid doing so whenever possible). Some examples:
var color = “red”;
var num = 25;
var visible = true;
05_579088 ch02.qxd 3/28/05 11:35 AM Page 11


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

R7