Today is:
    | Home | Web Technology | Language | Articles | | About-Us | Contact-Us |

JS Tutorial     

JavaScript Chapter 2 - Fundamentals


2.1 Embedding a script in an HTML document

You can embed JavaScript in the HTML document in one of two ways:
  • using <SCRIPT> tags, or
  • inserting inside an HTML tag to resonse to an event.

If JavaScript is embedded using a beginning and ending script tag, the browser starts by finding the first script tag and then reads everything that follows until the ending script tag. JavaScript translates each line of code into instructions to perform.

2.2 Events

Events are actions created by user actions on an HTML element. An event can trigger an execution of JavaScript codes to perform a desire goal. For example, we can use the onClick event of a button element to indicate that a JavaScript function will run when a user clicks on the button. The event is defined within an HTML tag.

Examples of events:

  • A mouse click
  • A web page or an image loading
  • Mousing over a hot spot on the web page
  • Selecting an input box in an HTML form
  • Submitting an HTML form
  • A keystroke

Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!

2.3 Basic syntax

2.3.1 Indentifier:

Identifiers are simply names that represent variables, methods, or objects. A syntactically-valid identifier must:

  • begin with either a letter (uppercase or lowercase alphabetical character, a through z) or an underscore (_)
  • be optionally followed by letters, digits or underscores.

2.3.2 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 decision based on the present state of the computer, or starting a repetitive loop inside your application.

The following is a list of keywords available in JavaScript:

break false if new this while
continue for in null true with
else function int return var

JavaScript version 1.1 added the following keywords: typeof and void.

JavaScript verion 1.2 adds the followign keywords: do, labeled, switch and while.

2.3.3 Reserved Words:

Reserved Words are identifiers tht you may not use as names for JavaScript variables, functions, objects or methods. The following is a complete list of the reserved words for JavaScript:

abstract continue function new this with
boolean default goto null throw
break delete if package throws
byte do implements private transient
case double import protected true
catch false in public try
catch final instanceof return typeof
char finally labeled short var
class float long static void
const for native synchronized while

2.3.4 Literals:

Literals are data comprised of numbers or string used to represent fixed values in JavaScript. The literals don't change during the execution of scripts. There are five types of literals: integer literals, floating-point literals, Boolean literals, String literials and special characters.

Integer Literals can be expressed in either decimal (base 10), octal (base 8), or hexadecimal (base 16) format. A zero infront of an integer literal designates octal form. The integer itself can include a sequecne of degits from 0 through 7. To designate hexadecimal, 0x (or 0X) is used before the integer. Hexadecimal integers can include digits from 0 through 9 along with the letters a through f or A through F. Here are example:

Decimal 33,2139
Octal 071,03664
Hexadecimal 0x7b8,0X395

Floating-Point Literals represent decimal numbers with fractional parts. They can be expressed in either standard or scientific notation. With scientific notation, use either e or E to designate the exponent. Both the decimal number and exponent can be either signed or unsigned (positive or nagative). Example:

3405.673
-9.928
8.3200e+11
8.2333e11
9.98E-12

Boolean Literials: JavaScript implements boolean data types and supports the two literals, true and false. They represent the Boolean vlaues 1 and 0 respectively, and must appear in lowercase.

String Literials: a string literial is zero or more characters enclosed in double quotes (") or single quotes (').

Special Characters: When writting scripts, you might sometimes need to tell the computer to use a special characters or keywords, such as tab or new line. All speciall characters are preceded by a backslash (\), as shown bellow:

  \b indicates a backspace.
\f indicates a form feed.
\n indicates a new line.
\r indicates a carriage return.
\t indicates a tab.
\\ indicates a backslash.
\' indicates a single quote.
\" indicates a double quote.


    References

    (1) Aland Shalloway & James R. Trott, Design Patterns Explained, Second Edition.

    (2) Allen Holub, Holub on Patterns, Learning Design Patterns by Looking at Code

    (3) Eric Evans, Domain-Driven Design, Tackling complexity in the heart of software.

    Advertisement

puthik.com ©2008