JavaScript Code Structure

JavaScript uses dot notation or object.method() syntax. There is a dot, or period, between each model path segment and before the method or property. Methods always have parentheses. When there is a choice of Properties, they are specified with square brackets, or with square brackets and quotes when the choice is a string or name.

The following table summarizes the parts of a JavaScript.

Parts of CodeExamples

Object Model Paths:

  • Start with an uppercase letter

  • Separate path segments with a period (.)

ActiveDocument.Sections.Count

is the correct syntax to access the Count property of the Sections in Active Document, while

ActiveDocumentSections.Count

generates the following error because the separator between ActiveDocument and Sections is missing:

ActiveDocumentSections is not defined

Methods (and Functions):

  • Separate from the object path with a period (.)

  • Include parentheses for arguments

Activate() does not take arguments, but the parentheses are still required.

ActiveDocument.Sections["RevSummary"].Activate()

The Add() method requires a single argument, included in the parentheses.

Time.Add(TextBox1.Text)

The Alert() method requires at least one argument and allows for multiple optional arguments. Multiple arguments are separated by commas.

Application.Alert(TextBox1.Text,"Text Box")

Properties:

  • Separate properties from objects with a period (.)

  • Refer to one of a collection of properties, by number, in brackets []

  • Refer to one of a collection of properties, by name, in brackets, with quotes [""]

When referring to the Count property of document sections, use:

ActiveDocument.Sections.Count

When referring to the first section (not the name, but the position in the section array in the object model), use:

ActiveDocument.Sections[1]

When referring to a specific section named RevSummary, use:

ActiveDocument.Sections["RevSummary"]

Statement Separators:

  • Statements must end with a return [Enter]

  • End statements with both a semicolon (;) and a return [Enter] to avoid JavaScript errors

  • Separate short statements on one line with a semicolon (;)

Statements can be on separate lines:

Time.Add(TextBox1.Text);
DropTime.Add(TextBox1.Text);

Multiple statements can be on one line, with a semicolon separating them:

Time.Add(TextBox1.Text);DropTime.Add(TextBox1.Text);

Comments

  • Use // for single line or inline

  • Use /* and  */ for multiple lines

	Time.Add(TextBox1.Text) // this is a comment
	// DropTime.Add(TextBox1.Text)
// this line and the line above are both comments
	
	/*
	Everything in here is a comment until the end comment
	marker.
	*/