JavaScript Syntax

Home

Script Tag

<script language="JavaScript1.2">
  code here
</script>

Comment

// Copyright 2000 Martin Budden

Declare And Set Variable

var MyVar1 = true

Declare Function

function MB_MyFunctionName2(argument1, argument2){
  // execute code here
}

Call Functions (Example: onLoad Event)

onLoad="MB_MyFunctionName1(); MB_MyFunctionName2(argument1, argument2)"

If

if (MyVar1 == MyVar2) {
  // execute code here
} else if (MyVar1 == MyVar3) {
  // execute code here
} else {
  // execute code here
}

For Loop

for ([initial expression]; [condition]; [update expression]) { }

For Loop Example:

for (var i = StartValue; i <= MaxValue; i++) { // Note that ++ adds 1 to i at END of each loop
  // execute code here
}

While Loop

while (condition) {
  // execute code here
}

Switch

switch (condition) {
  case "value1":
    // execute code here
    break // Optional: skips checking other values
  case "value2":
    // execute code here
  default: // Optional: if no values match
    // execute code here
}

Try - Catch

try {
  // execute code here
} catch(e) {
  // error handling here
}

Object Property Reference Example

document.FormName.TextFieldName.value

Convert Case Function Example

document.FormName.TextFieldName.value.toUpperCase()

Boolean Comparisons

==  Equals
!=  Does not equal
>   is greater than
>=  is greater than or equal to
<   is less than
<=  is less than or equal to

Boolean Operators

&&  And
||  Or
!   Not

Maths

+   Add (numbers) or Concatenate (strings)
-   Subtract
*   Multiply
/   Divide
++  Increment
--  Decrement