# Data Types

Arrays one-dimensional arrays.

> *array_name* = new Object()

Boolean *true* or *false*

BMattIsCool is false;

Functions belong to objects in JavaScript; also called *methods*

Function funMakeMattCool()

{

bMattIsCool := true;

return bMattisCool;

}

Numbers numeric data type, for computations.

Var nCounter = 0;

Objects something that other things belong to. Usually interchangeable
with arrays.

oObject = new Object();

Date Predefined object with methods for processing. Holds date
information

Math Predefined object with mathematical methods

Strings String values

var cText := "Matt is cool";

# Special Values

false boolean false

null No value assigned. Seems to be a distinct value, not the same as ""
(empty string)

NaN Not a Number. Not equal to anything, including itself (like ORACLE
null)

true boolean true

undefined Object or property has not been defined (occurs when
referenced)

# Conversion Examples

Number to string cAmount = '\$' + nAmount; //changes nAmount to string,
concatenates to '\$'

cAmount = nAmount.toString(); //base 10; default

cAmount = nAmount.toString(16) //base 16

String to number nAmount = cAmount -- 0; //arithmatic operation. "+" is
concatenation operator

# Operators

Arithmetic Operators

\+ Addition

\- Subtraction

> \* Multiplication (should be asterisk)

/ Division

\% Modulus

++ Increment

\-- Decrement (should be 2 minus signs)

& Bitwise AND

\| Bitwise OR

\^ Bitwise XOR

\<\< Shift bytes left (integer only)

\>\> Shift bytes right with sign extension (integer only)

\>\>\> Shift bytes right with zero extension (integer only)

Assignment Operators

= Assignment

+= Add and assign

-= Subtract and assign

\*= Sultiply and assign

/= Divide and assign

%= Modulus and assign

\<\<= Shift bytes left and assign

\>\>= Shift bytes right with sign extension and assign

\>\>\>= Shift bytes right with zero extension and assign

&= Bitwise AND operator and assign

\|= Bitwise OR operator and assign

\^= Bitwise exclusive OR operator and assign

Logical Operators

&& Logical AND

\|\| Logical OR

! Negation

== Equality (2 equal signs; don't confuse with assignment, "=")

\< Less than

> \> Greater than (should be greater than sign)
>
> \>= Greater than or equal to
>
> \<= Less than or equal to
>
> != Not equal to

Miscellaneous Operators

. object.property identifier

\[\] array element identifier (subscript must be integer)

new Constructor call

typeof returns datatype of argument

if (typeof txtAmount == "object")

void returns undefined value

return void;

?: conditional (ternary) operator. Compares 2 values. If condition is
true,

first value is returned, otherwise second. Basically a condensed IF
ELSE,

similar to ORACLE's DECODE()

Syntax is *condition* ? *true value* : *false value*

bIsMattCool = ("Matt is Cool" == sText ? true : false);

String Operators

\+ Concatenation (be careful not to confuse with addition)

# Commands

Expressions Simple expressions that have side effects (usually
assignments)

bMattIsCool = true;

Compound Multiple statements in a block of code (between "{" and "}"

{

> bMattIsCool = true;
>
> alert("Is Matt coool?"+bMattIsCool);

}

; Command terminator. By itself, is an empty (no operation) statement

if (bMattIsCool)

;

else

alert("Matt is not cool");

break exit current for or while loop

j = 0;

for (i=0;i\<9;i++)

{

> j = j \* i;

if (j \> 1000)

break;

}

continue go to the top of the current loop

j = 0;

for (i=0;i\<9;i++)

{

if (j \> 1000)

continue;

> j = j \* i;

}

for Numeric for loop. 3 parts: index initialization, comparison, and
increment. If

multiple statements are to be executed a compound statement must be used

j = 0;

for (i=0;i\<9;i++)

j = j \* i;

for (i in *object*) UNIX Bourne shell style for loop, used to retrieve
property values (names)

from objects. Value in index is a string. If more than one command is to

be executed, a compound statement must be used

for (i in document)

alert(i);

function function declaration

function DoAlert(Value)

{

alert(Value);

}

if If condition. If more than one command is to be affected by the if,

a compound statement must be used. Parenthesis are mandatory

if (text == "Matt is cool")

bIsMattCool = true;

return exit the current function and return the specified value

return isTrue;

var variable declaration. Not always necessary, declaring variables is
advisable

var isTrue = false;

var Counter = 17;

var Text = "";

while while loop. If more than one command is to be executed, a compound

statement must be used

while (Text.length \> 0)

Text = Text.subststring(2,text.length-1);

# Predefined Objects and Properties

window

frames\[\]

document

*form*

*form fields*

# Objects, Properties,and Methods

Date object

new Date() Creates a new date object with current date and time.
Components may be

specified

oDate = new Date();

oDate = new Date(sYear,sMonth,sDay);

oDate = new Date(sYear,sMonth,sDay,sHours,sMinutes,sSeconds);

getDate() no arguments. Returns month of a date object

sMonth = oDate.getDate();

getDay() no arguments. Returns day of week of a date object

sDay = oDate.getDay();

getMonth() no arguments. Returns month of year (0-11)

sMonth = oDate.getMonth();

getyear() no arguments. Returns year as 4 digit string.

sYear = oDate.getyear();

setDate() Set the day of month of a date object

oDate.setDate(sDay);

setMonth() set the month field of a date object

oDate.setMonth(sMonth);

setYear() set the year field of a date object

oDate.setYear(sYear);

Document object

onload executes indicated code when document is fully loaded. Located in
body tag.

\<BODY onLoad="jsonLoad();"\>

onunload executes indicated code when document is unloaded. Located in
body tag.

\<BODY onunload="jsonload();"\>

Element object

defaultValue Readonly string specifying a form field's default value

factors.txtAmount\[1\].value = factors.txtAmount\[1\].defaultValue';

form Recursive reference to forms object element belongs to

alert(oField.form.name);

type type of form element

if (oField.type == "TEXT")

value the (string) value to be sent to the server for this element

if (oField.value == "")

blur() Removes keyboard focus from an element. Does not invoke onBlur();

oFactors.txtAmount.blur();

click() Simulate a mouse click event Does not invoke onClick();

oFactors.cbDelete.click();

focus() Give keyboard focus to an element. Does not invoke onFocus();

oFactors.cbDelete.focus();

onBlur() Invoked when the element loses focus. Included in field
definition

\<INPUT TYPE="TEXT" NAME="txtAmount" onBlur="jsOnBlur();"

onFocus() Invoked when the element gains focus. Included in field
definition

\<INPUT TYPE="TEXT" NAME="txtAmount" onFocus="jsOnFocus();"

onchange() Invoked when an element change value is detected

\<INPUT TYPE="TEXT" NAME="txtAmount" onChange="jsOnChange();"

onClick() Invoked when a clickable element is clicked

\<INPUT TYPE="CHECKBOX" NAME="cbDelete"

onClick="jsOnClick();"

Forms object

elements\[\] array containing elements (fields defined) for form. If the
form elements are

scalar, they can be referred to as *forms.fieldname*

alert(oDateForm.field1.name);

alert(factors.txtAmount\[1\].value);

length number of fields defined for the form

nFields = oFactors.length //oFactors is a forms object

reset() resets each input element of form. Reset button processing

oFactors.reset(); //oFactors is a forms object

onreset executes JavaScript code when reset event occurs

\<FORM NAME="oFactors" onReset="jsonreset();"\>

onsubmit execute Javascript code when submit event occurs

\<FORM NAME="oFactors onSubmit="jsonSubmit();"\>

elements\[\] array with elements (input fields) of HTML form

this the current field field being defined

\<INPUT TYPE="TEXT" NAME="Amount" onChange="jsOnChange(this);"

Math Object

Constants A variety of predifined constants

nPi = Math.PI;

abs() Absolute value

nAbsAmount = Math.abs(nAmount);

ceil() The smallest integer \>= its argument. Does not really round

nIntAmount = Math.ceil(nAmount);

floor() The largest integer \<= its argument. Does not really round

nIntAmount = Math.floor(nAmount);

max() Return the larger of 2 numbers (probably variables)

NMax = Math.max(nAmount1,nAmount2);

min() Return the smaller of 2 numbers (probably variables)

Nmin = Math.min(nAmount1,nAmount2);

round() Rounds to the nearest integer

Nint = Math.round(nAmount);

Number object

MAX_VALUE the largest possible number

if (nCounter == Number.MAX_VALUE)

MIN_VALUE the smallest possible number

if (nCounter == Number.MIN_VALUE)

NaN Not a Number value. Cannot be detected with "=="

if isNan(nCounter)

POSITIVE_INFINITY positive infinity value; returned on an overflow

if (nCounter == Number.POSITIVE INFINITY)

NEGATIVE_INFINITY Negative infinity value; returned on an underflow

if (nCounter == Number.NEGATIVE INFINITY)

toString() converts the number to a string, using a specified base

sCounter = nCounter.toString(10); //10 is optional for base 10

Object object

eval() evaluate JavaScript code to a string (dynamic code)

oObject = eval(nFormName + "." + nFieldName);

toString() convert object value to string

sValue = nValue.toString();

valueOf() returns primitive value of specified object

if (oDate1.valueOf() == oDate2.valueOf())

String object

charAt() return the nth character of a specified string

sChar = sPath.charAt(4);

indexOf returns the position of a character in a string

nPosition = sPath.indexOf("/");

length the length of the string

nLength = sPath.Length;

lastIndexOf returns the last position of a character in a string

nPosition = sPath.lastIndexOf("/");

split break a string into an array by a specified delimiter

a = sPath.split("/");

subString() returns a substring. Not like other substring functions,

its arguments are only from and to.

nChar = sPath.substring(0,1); //1 greater than last position

toLowerCase() converts string to lower case

if (sPath.toLowerCase() == "/tmp")

toUpperCase() converts string to upper case

if (sPath.toUpperCase() == "\\WINDOWS")

Window object

closed Boolean variable identifying if the window is closed

document the document object within the window

frames\[\] An array of frames used by the window

Name (Property) a string whose value is the name of the window

opener refers to the window that opened the current window

self reference to the window itself; this window

status string variable that specifies the content of the status line

top reference to the top level window

alert() puts its argument on the screen in an alert window.

alert("Matt is cool");

blur() take keyboard focus from the top-level browser window

window.blur();

close() close a window. Mostly used with windows opened with open

window.close();

open() open a new window

w = window.open();

onBlur() Event handler invoked when window loses focus

onerror() Event handler invoked when a JavaScript error occurs

onfocus() Event handler invoked when window gains focus

onLoad() Event handler invoked when document or frameset is fully loaded

onUnload() Event handler invoked when doccument or frameset is discarded
by browser