Syntax Conventions Description
Case sensitivity
myvariable is not the same as myVariable
--------------------------------------------------
var
All JavaScript variables and JavaScript functions must be identified with unique names called identifiers. Identifiers can be short names like x and y or they can be more descriptive like dateOfBirth or lastName.
The var keyword is used to create new variables. Variables are containers for storing data values.
Rules for creating variable or function names in JavaScript:
- Names must begin with a letter, $, or _
- Names can contain letters, digits, underscores, and dollar signs
- Names are case sensitive (y and Y are different variables)
- Reserved words (like JavaScript keywords) cannot be used as names
Best Practice:
When declaring variable and function names, use camel case methodology, which combines multiple words together always starting with a lower case letter and with each successive word starting with a capital letter.
Examples:
dateOfBirth
lastName
If using abbreviations, they should still be descriptive.
Example:
var lastName;
--------------------------------------------------
=
The equal sign (=) is used to assign values to variables.
Example:
var lastName = 'Watts';
--------------------------------------------------
;
All line statements must end with a semicolon character (;). As a general rule, block statements do not end with a semicolon.
--------------------------------------------------
Simple Statements
Simple statements perform one and only one function.
Example:
message.setNode('MSH-3', 'Radiology');
This statement hardcodes or sets the word 'Radiology' into the MSH-3 field of an HL7 message.
--------------------------------------------------
Compound Statements
Compound statements are comprised of multiple functions.
Example:
message.setNode('MSH-3', StringUtils.upperCase('radiology'));
This statement first changes the word 'radiology' to uppercase and then sets it into the MSH-3 field of an HL7 message.
--------------------------------------------------
Block Statements
Block statements start and end with curly brackets and are widely used with if statements, for loops, and other JavaScript functions.
There can be multiple simple or compound statements within a block.
Example:
{
var sendingApp = StringUtils.upperCase('radiology');
message.setNode('MSH-3', sendingApp);
}
--------------------------------------------------
White Space
JavaScript ignores extra spaces, allowing white space to be inserted into code to make it more readable.
However, if there is white space at the end of a line, the QIE JavaScript validation tool will flag it as a syntax error.
The error message will read:
line x col true – Trailing whitespace
Removing the extra spaces at the end of the indicated line will eliminate the error.
--------------------------------------------------
Splitting code across multiple lines
If a line of code does not fit on one line, it is best to break the line after an operator or comma, or within a string of text.
Example:
if (StringUtils.equalsIgnoreCase('CPS10', centricityVersion)) {
message.setNode("/", qie.callWebService(
channelCache.getValue('wsName'),
"CCD Export",
parameterMap));
}
In this example:
- The line is split at the opening parenthesis (
- Additional lines split after commas
- The statement ends with the closing parenthesis and semicolon
--------------------------------------------------
' and "
Single and double quotes are interchangeable within JavaScript, but must be matched within a given statement.
Examples:
'text'
"text"
A value that starts with a single quote must end with a single quote.
Quotes are typically used to surround textual values (strings and literals).
--------------------------------------------------
+
The plus symbol (+) has different meanings in JavaScript.
- With numerical values, it performs arithmetic addition
- With strings, it concatenates values together
Example:
var string1 = "Nothing is impossible, \n\r";
var string2 = "the word itself says I'm possible! \n\r";
var string3 = "Audrey Hepburn";
var quote = string1 + string2 + string3;
Result:
Nothing is impossible,
the word itself says I'm possible!
Audrey Hepburn
--------------------------------------------------
// Single line comments
Single line comments are made using two forward slashes.
Examples:
// This is a single line comment
var x = 5; // this is an inline comment
--------------------------------------------------
/* */ Multiple line comments
Multiple line comments start with:
/*
and end with:
*/
All lines between the opening and closing comment characters will be commented out.
Example:
/*
var x = '5';
var y = '3';
var z = x + y;
*/