Programming Languages
A Dave Hillman Project

[Home] [Contact Me] [Site Map] [Search] [References]

JavaScript

 

[Up]

[Language Basics]
[C Language]
[C++]
[C#]
[Java]
[JavaScript]
[Visual Basic 6.0]
[VBScript]
[VB.Net]
[.Net Technology]

 

 

Introduction

Web page scripting via JavaScript:

bulletThe script is contained within the HTML of the web page. It may be found in a script tag or as as DHTML code tied to a form event (or other CSS event).
bulletClient scripts are "programs" that run within the browser environment.
bulletThe programming languages are relatively powerful with data, control, and structuring syntax in addition to being able to access object-based features of the web browser.
bulletThe script is processed by a "scripting engine" that is part of the browser.
bulletThe scripting engine has access to the web document object, HTML and form objects, and many system functions (date, time, ability to access cookies)
bulletScript engines may not read and write other files (security reasons primarily) in the operating system of the client computer.
bulletImplementation of the scripting engine is operating system (to some degree) and browser (more so) dependent.
bulletThere are subtle differences in JavaScript for Netscape and JScript for Internet Explorer.

Language Fundamentals

JavaScript is not related to Java, however, like Java, it shares much of it's syntax with C.

Data Types

JavaScript supports three primitive data types:

  1. Boolean - true, false
  2. Number - all numbers
    1. Integers
    2. Hex and Octal
    3. Floating Point
  3. String - typically quoted

JavaScript also supports two composite data types:

  1. Object
  2. Arrays (of numbers, strings, Boolean, objects)

In addition JavaScript supports two trivial data types:

  1. Null
  2. Undefined

JavaScript is a weakly typed language in that ALL variables are not declared as a specific data type. 

Variables are declared by the "var" statement and may be overloaded with a value (e.g., var vname = 12;)

Functions

JavaScript has two types of functions:

  1. User-defined - created by the user (can include a unique name, input parameters, and returned data).
  2. Built-in - including the following:
bulletEscape - encodes the string that is contained in the string argument to make it portable (a string is considered portable if it can be transmitted across any network to any computer that supports ASCII characters).  To make a string portable, characters other than the following 69 ASCII characters must be encoded:
 
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
1234567890
@*-_+./
 
All other characters are converted either to their two digit (%xx) or four digit (%uxxxx) hexadecimal equivalent.
 
bulletEval - evaluates and/or executes a string of JavaScript code.
bulletisFinite - used to determine if an argument is a finite and legal number.
bulletisNaN - used to determine if an argument is a NaN.
bulletNumber - converts the object argument to a string representing the object's value, if the value cannot be represented by a legitimate number, the "Not-a-Number" value, NaN is returned.
bulletparseFloat - determines if the first character in the string argument is a number, parses the string from left to right until it reaches the end of the number, discards any characters that occur after the end of the number, and finally returns the number as a number (not as a string).
bulletparseInt - find the first integer in a string.  There are two arguments. The mandatory string argument is the string which is presumed to contain an integer. The optional radix argument specifies the base of the integer and can range from 2 to 36. For example, 16 is the hexidecimal base (0123456789ABCDEF).
bulletString - converts the object argument to a string representing the object's value.
bulletUnescape - decodes an encoded string argument that was created using the escape function

Objects

Many of these are/can be named objects (you can define a local name, e.g., var mystring = "hello world", mystring is a string object).

bulletAnchor - target of a hyperlink
bulletApplet - embedded applet in a web page (often Java)
bulletArea - same as a link
bulletArray - built-in support for arrays, includes length property; also includes concat, join, pop and other methods.
bulletBoolean - support for boolean values
bulletButton - graphic pushbutton
bulletCheckbox - graphic checkbox
bulletDate - manipulate date and time values
bulletDocument - HTML document window management, numerous properties for document objects (links, forms, etc.); includes write method for output to browser window
bulletEvent - details about an event
bulletFileUpload - file upload for form input
bulletForm - HTML input form (typically superior to form objects)
bulletFrame - type of window object
bulletFunction - a JavaScript function
bulletHidden - hidden data field access
bulletHistory - URL history of the browser
bulletImage - image in a web browser document
bulletLayer - independent layer in a DHTML document
bulletLink - hypertext link
bulletLocation - represents and controls browser location (current URL, host port, and protocol information)
bulletMath - manages mathematical functions (absolute value, trig functions, random, round, etc.) and constants (.E, .LN10, .PI, etc.) . 
bulletNavigator - information about the current browser (type, platform, etc.).
bulletNumber - support for numbers (convert to string, is a number).
bulletObject - superclass that contains features of all JavaScript objects.
bulletOption - an option in a select box.
bulletPassword - specialized input box.
bulletRadio - graphical radio button.
bulletReset - graphical button for resetting a form.
bulletScreen - provides information about the display.
bulletSelect - a graphical selection list.
bulletString - support for strings include replace, search, concatenation, and other manipulation methods.
bulletSubmit - graphical button for triggering a form submission.
bulletText - graphical text input field.
bulletTextarea - graphical multi-line text input area.
bulletWindow - web browser window or frame.

Expressions and Operators

bulletArithmetic
   =
+, ++
-,  --
   /
   %
bulletAssignment
   =
bulletBackslash Escaped Characters
   \'
   \"
   \\
   \b
   \f
   \n (newline)
   \r
   \t
bullet Bitwise
   &
   |
   ^
   ~
   <<
   >>
   >>>
bulletComparison
   ==
   !=
   ===
   !==
   >
   >=
   <
   <=
bullet Logical
   && (and)
   || (or)
   ! (not)
bulletSpecial
   ?:
   '
   delete
   new
   this
   typeof
   void
bulletString
   +  (concatenate)

Control Statements

bulletIf...Else - executes one set of statements if a specified condition is true, and another if it is false.
bulletEvaluate statements if expression is true for if condition or subsequent else conditions.
bulletIncludes if, if-then logic; complex Boolean evaluation.
bulletShort-circuit if first logical expression is found false.
bulletExamples:

if (expression) statement;

if (expression)  {
    statement;
    statement;
    ....  }

if (expression)  {
    statement;
    statement;  }
else
     statement;

if (expression)  {
    statement;
    statement;  }
else  {
    statement;
    statement;  }

if (expression)  {
    statement;
    statement;  }
else if (expression)
     statement;

 

bulletFor - creates a loop consisting of three optional expressions enclosed in brackets and separated by semicolons, and a block of statements to be executed.

for (expression1; expression2; expression3)
        statement...

expression1 - initial value
expression2 - goal value
expression3 - modify value or function

 

bulletFor...In - used to iterate a declared variable over every property in a specified object.

for (variable in object)  {
    statements...
}

bulletDo...While - executes one or more statements at least once, checking that a certain condition is met each time before repeating. If that condition is not met, then control moves to the statement immediately after the loop.

do {
    statement;
    statement;
    ...  }
while (expression)

 

bulletWhile - creates a loop consisting of a block of statements that is executed if the expression evaluated is true.


while (expression) {
    statement;
    statement;
    ...  }

bulletWith - establishes a default object for a set of statements. If there are any unqualified names in a set of statements, JavaScript first checks the default object to see if they exist there as properties of that object; otherwise a local or global variable is used.

with (object)  {
   statements...
}

bulletSwitch - tests an expression against a number of case options and executes the statements associated with the first one to match.

switch (expression) statement

switch ( command )
    case 'A': statement
    case 'B': statement

 

Other statements...

bulletFunction - declares a function with its specified parameters, which can include numbers, strings and objects. To return a value, a function must have a return statement specifying the value to return.
bulletBreak - used to terminate a current loop, switch or label statement and pass control to the statement immediately following it
bulletComment - programmer notes, they are ignored by the JavaScript interpreter
bulletContinue - restarts a while, do...while, for or label statement, in a while loop it jumps back to the condition.
bulletExport - allows a signed script to provide properties, functions and objects to other signed or unsigned scripts.
bulletImport - allows a script to import properties, functions and objects exported by a signed script.
bulletLabel - used to identify a statement allowing you to refer to it elsewhere in a program, typically used with the break or continue statements.
bulletReturn - specifies the value to be returned by a function and performs the act of returning that value to where the function was called from.
bulletthrow - allows the programmer to create an exception. This exception can be a string, integer, Boolean or an object.
bullettry...catch - used to test a block of code for errors. The try block contains the code to be run, while the catch block contains the code to execute if there is an error.
bulletVar - used to declare a variable, and outside of a function its use is optional.
 

Input/Output

bulletJavaScript, within the web browser environment, is dependent upon the browser (via HTML) for user input/output.
bulletFile I/O is limited due to security considerations.  However, file access can be accomplished via system file objects (web server only).
bulletJavaScript does not have database access capability in the browser.

Note: there are environments where JavaScript syntax is used where additional features exist.  These include web servers (via Active Server Pages, ASP) where file and database I/O is accomplished via object interfaces.

Web Sites for JavaScript

bullet www.javascript.com - Netscape sponsored, references and  tutorials are available
bullet http://msdn.microsoft.com/scripting/default.htm - Microsoft sponsored, references and  tutorials are available
bullet http://www.builder.com/Programming/Javascript/?st.bl.fd.ts3.feat.1711 - a JavaScript tutorial on CNET.com
bullethttp://www.devguru.com/ DevGuru: an excellent reference on JavaScript

 

[Home] [Language Basics] [C Language] [C++] [C#] [Java] [JavaScript] [Visual Basic 6.0] [VBScript] [VB.Net] [.Net Technology]

Copyright © 2003-2005 by Dave Hillman