Basic rules of JavaScript

The following rules apply to the basic syntax of JavaScript:

  • JavaScript is case-sensitive.
  • Statements should end in a semicolon (;).
  • Variables:

    • Must be defined before being used. The variable name can contain A – Z, a – z, underscore or digits and must start with a letter or an underscore (“_”).
    • Assume the type of the data that is put into the variable. The data type does not have to be explicitly defined
    • Are global variables when defined outside of a function, and are available anywhere in the current script context. Variables created within a function are local variables, and can be used only within that function.
  • Strings have to be enclosed in quotation marks, either a single or double. For example: print(”Hello ” + ‘world ‘+ Country.name) produces the following: Hello world US.
  • Special characters that are displayed literally must be preceded by a backslash character (\). Quotes within a string can be entered preceded by a backslash as well. See the Core Language Features - Literals - "String literals" section for more information in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide.
  • To increment a variable, such as a = a + 1, you can use a++. You can decrement a variable in the same way, as in a--.
  • To enter comments in the script, use "//" to start a single line comment or the combination of "/*" and "*/" to enclose a multi-line comment.
  • Values that are not defined as a data type (string, number, Boolean) may be defined as an object, such as Date, Array, Boolean, String, and Number. As an example you could define: var ArrayList=new Array(”test”, ” this”, ” list”);.
  • Dots in Service Manager field names must be replaced by an underscore (_) in JavaScript. For example contact.name becomes contact_name.
  • Service Manager field names that are reserved words in JavaScript have to be preceded by an underscore, such as “_class” for the “class” field.

Structure of a JavaScript application

Refer to the following example for the structure of a JavaScript application.

<Global variable declarations;>
function <function name>(<parameters>)
{
	<local variable declarations;>
	<Statements;>
}
<function calls;>

Expressions and operators

For more information on this topic, refer to the Expressions and Operators section in:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide

The list of operators includes assignment operators, comparison operators, arithmetic operators, bitwise operators, logical operators, string operators, and special operators.

JavaScript objects and properties

The key to understanding JavaScript is learning the structure of its objects. Most JavaScript functionality is contained in objects. The objects contain methods, parameters, and events. Each object may contain a set of properties that more closely define the object. The syntax you use to call methods and properties is: Object.method or Object.property.

For more information about JavaScript objects and properties refer to the Details of the Object Model section in :

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide

Built-in objects and functions

Built-in objects described in this section are Array, Boolean, Date, Function, Math, Number, and String.

Each of these is described in detail in the Working With Objects – Predefined Core Objects section of:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide

Conditional statements

The following example returns true if the date entered is within the number of days in a month (here, 30 days); otherwise, it returns false.

Note Note the use of the conditional statement to shorten the "if" expressions. The var return = (parm.Date.getDate <= 30) ? true : false; statement works in the same way as its much longer equivalent:

var result=false;
if (parm.getDate()<= 30)
{
result = true;
}
else
{
result=false;
}

While and loop statements

Note Whenever programming a loop statement, ensure that the exit condition will be met; otherwise, an infinite loop will occur.

For detailed information about the for and while loop statements, refer to the Statements – Loop Statements section in:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide

The following example shows how to check how many times a given number can be divided by 2, first using a for loop, and then using a while loop:

function DivisableByTwoFOR(ParmNumber)
{
	for (var i=0; ParmNumber > 1; i++)
	{
		ParmNumber = ParmNumber / 2;	
	}
return i;
}
function DivisableByTwoWHILE(ParmNumber)
{
	var i = 0;
	while( ParmNumber > 1 )
	{
		ParmNumber = ParmNumber / 2;
		i++;
	}
return i;
}

Note A Do..While loop is available for those instances when you want to execute a statement (or block of statements) at least once. In a Do..While loop, the exit condition is checked after the statements are executed.

Caution The for…in loop over an array may return unexpected results. For details, see Avoiding the use of the for…in loop over an array.

Break and continue statements

The break statement terminates a while or for loop completely. The continue statement terminates execution of the statements within a while or for loop and continues the loop in the next iteration. The following two examples demonstrate how these statements are used.

Note Both these statements violate the rules of structured programming but are nonetheless widely used in special cases.

function break4error(parmArray, x)
{
    var i = 0;
    var n = 0;
    while (i < 10)
    {
	if (isNaN(parmArray[x]))
    {
	print(parmArray[i].toString + “ is not numeric. Please repair
	and run again”);
	break;
    }
       n = n + parmArray[i];
       i++;
    }
    return n;
}

Modifying the above example, the continue statement would work as follows:

function break4error(parmArray, x)
{
    var i = 0;
    var n = 0;
    while (i < 10)
    {
	if (isNaN(parmArray[x]))
	{
	    print(parmArray[i].toString + “ is not numeric. Continuing with 
	    next number”);
	    i++;
	    continue;
	}
	    n = n + parmArray[i];
	    i++;
	}
	return n;
}

Exception handling statements

As a best practice, exception handling should always be included in any JavaScript program. The following code provides an example of using exception handling statements for JavaScript.

function SetFullName(FirstName,LastName) 
{
	if (FirstName != null || LastName != null) 
	{
		FullName=FirstName + “ “ + LastName
		return FullName
	} 
	else 
	{
		throw "InvalidName"
	}
}
try 
{
// statements to try
 var MyName=SetFullName(First, Last) // function could throw exception
}
catch (e) 
{
 MyName="unknown"
 logMyErrors(e) // pass exception object to error handler
}

For detailed information about exception handling, refer to the Statements – Exception Handling Statements section in:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide